Spring mvc. case insensitive get parameters mapping

后端 未结 3 612
温柔的废话
温柔的废话 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 15:55

    Actually, you have to change the things in the CaseInsensitiveRequestFilter class as per your bean variable name. In your case the variable is valueName, so for every request it will convert it as per your variable setter method camel case injection and then match as per your request. Just try for your custom requirements:

    package biz.deinum.web.filter;
    
    import java.io.IOException;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.util.LinkedCaseInsensitiveMap;
    import org.springframework.web.filter.OncePerRequestFilter;
    
    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<>();
    
            private CaseInsensitiveHttpServletRequestWrapper(HttpServletRequest request) {
                super(request);
                Map map = request.getParameterMap();
                Set set = map.entrySet();
                Iterator it = set.iterator();
                Map tempMap = new HashMap(); 
                while (it.hasNext()) {
                    Map.Entry entry = (Entry) it.next();
                    String key = entry.getKey();
                    // Keep your parameter bean name here in your case it is "valueName"
                    String beanParamaterName = "valueName";
                    if(key.equalsIgnoreCase(beanParamaterName)){
                        tempMap.put(key.toLowerCase(), entry.getValue());
                    }
                }
                params.putAll(tempMap);
            }
    
            @Override
            public String getParameter(String name) {
                String[] values = getParameterValues(name);
                System.out.println(values.toString()+"-");
                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) {
                System.out.println(name);
                return (String[])params.get(name);
            }
        }
    }
    

提交回复
热议问题