Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring mvc. case insensitive get parameters mapping

according this answer I try to write my code:

pojo:

class MyBean{

    public String getValueName() {
        return valueName;
    }

    public void setValueName(String valueName) {
        this.valueName = valueName;
    }

    String valueName;
}

inside controller:

    @ModelAttribute
    public MyBean createMyBean() {
        return new MyBean();
    }
    @RequestMapping(value = "/getMyBean", method = RequestMethod.GET)
    public String getMyBean(@ModelAttribute MyBean myBean) {
        System.out.println(myBean.getValueName());
        return "pathToJsp";
    }

web.xml configuration:

<filter>
    <filter-name>caseInsensitiveFilter</filter-name>
    <filter-class>com.terminal.interceptor.CaseInsensitiveRequestFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>caseInsensitiveFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Filter:

@Component
public class CaseInsensitiveRequestFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        filterChain.doFilter(new CaseInsensitiveHttpServletRequestWrapper(request), response);
    }

    private static class CaseInsensitiveHttpServletRequestWrapper extends HttpServletRequestWrapper {

        private final LinkedCaseInsensitiveMap params = new LinkedCaseInsensitiveMap();

        /**
         * Constructs a request object wrapping the given request.
         *
         * @param request
         * @throws IllegalArgumentException if the request is null
         */
        private CaseInsensitiveHttpServletRequestWrapper(HttpServletRequest request) {
            super(request);
            params.putAll(request.getParameterMap());
        }

        @Override
        public String getParameter(String name) {
            String[] values = getParameterValues(name);
            if (values == null || values.length == 0) {
                return null;
            }
            return values[0];
        }

        @Override
        public Map getParameterMap() {
            return Collections.unmodifiableMap(this.params);
        }

        @Override
        public Enumeration getParameterNames() {
            return Collections.enumeration(this.params.keySet());
        }

        @Override
        public String[] getParameterValues(String name) {
            return (String[]) params.get(name);
        }
    }
}

In debug I see that filter method invoke but I cannot achieve case insentive get parameters mapping.

for example localhost:8081/getMyBean?valueName=trololo works but localhost:8081/getMyBean?valuename=trololo - not

like image 930
gstackoverflow Avatar asked Sep 02 '15 22:09

gstackoverflow


1 Answers

Your problem, I believe, is @ModelAttribute. You're asking Spring to map the parameters to MyBean object, and the property inside this object is valueName.

In order Spring to reflect the params to the object, it needs to be in the correct case.

You have 2 options:

  • change the property name to valuename in your MyBean object, and all property names to lowercase, it should work with help of your solution.
  • remove @ModelAttribute and put @RequestParam for each property. If you have many props, this could be painful.
like image 66
Gokhan Oner Avatar answered Sep 21 '22 18:09

Gokhan Oner