Spring mvc. case insensitive get parameters mapping

后端 未结 3 614
温柔的废话
温柔的废话 2021-01-11 15:52

according this answer I try to write my code:

pojo:

class MyBean{

    public String getValueName() {
        return valueName;
    }

    public void          


        
3条回答
  •  借酒劲吻你
    2021-01-11 16:16

    Here is what you can do...

    Create the domain(POJO) with all lowercase variables

    public class MyBean{
        private String valuename;
    
        public String getValuename() {
            return valuename;
        }
    
        public void setValuename(String valuename) {
            this.valuename = valuename;
        }
    }
    

    Then create a class which will extend HttpServletRequestWrapper

    public class CustomWrappedRequest extends HttpServletRequestWrapper
    {
        private final Map modifiableParameters;
        private Map allParameters = null;
    
        public CustomWrappedRequest(final HttpServletRequest request, 
                                                        final Map additionalParams)
        {
            super(request);
            modifiableParameters = new TreeMap();
            modifiableParameters.putAll(additionalParams);
        }
    
        @Override
        public String getParameter(final String name)
        {
            String[] strings = getParameterMap().get(name);
            if (strings != null)
            {
                return strings[0];
            }
            return super.getParameter(name);
        }
    
        @Override
        public Map getParameterMap()
        {
            if (allParameters == null)
            {
                allParameters = new TreeMap();
                allParameters.putAll(super.getParameterMap());
                allParameters.putAll(modifiableParameters);
            }
            return Collections.unmodifiableMap(allParameters);
        }
    
        @Override
        public Enumeration getParameterNames()
        {
            return Collections.enumeration(getParameterMap().keySet());
        }
    
        @Override
        public String[] getParameterValues(final String name)
        {
            return getParameterMap().get(name);
        }
    }
    

    Last add a filter with appropriate web.xml config the doFilter() will look like this

    public void doFilter(ServletRequest request, ServletResponse reponse, FilterChain chain)
                throws IOException, ServletException {
            Map params = request.getParameterMap();
            Map extraParams = new TreeMap();
            Iterator i = params.keySet().iterator();
    
            while ( i.hasNext() )
              {
                String key = (String) i.next();
                String value = ((String[]) params.get( key ))[ 0 ];
                extraParams.put(key.toLowerCase(), new String[] {value});
    
              }
            HttpServletRequest wrappedRequest = new CustomWrappedRequest((HttpServletRequest)request, extraParams);
    
            chain.doFilter(wrappedRequest, reponse);
    
        }
    

    Here the filter will convet the params into lowercase and attach it to your custom made request. Then use can use @ModelAttribute in the controller code to get the desired object.

    Hope it helps :)

提交回复
热议问题