Spring Mvc Url和参数名称忽略大小写

在开发过程中Spring Mvc 默认 Url和参数名称都是区分大小写的

  比如:www.a.com/user/getUserInfo?userId=1 

     www.a.com/user/getuserInfo?userId=1 

       www.a.com/user/getUserInfo?userid=1 

            www.a.com/user/getuserinfo?userid=1 

  这些都认为不同的地址和参数,在实际中用户根本不区分这些,所以我们要忽略大小写

URL忽略大小写

import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * Created by tianwei on 2017/6/22.
 */
@Configuration
public class SpringWebConfig extends WebMvcConfigurationSupport {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher pathMatcher = new AntPathMatcher();
        pathMatcher.setCaseSensitive(false);
        configurer.setPathMatcher(pathMatcher);
    }

}

参数名称忽略大小写

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
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 CaseInsensitiveRequestParameterNameFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        filterChain.doFilter(new CaseInsensitiveParameterNameHttpServletRequest(request), response);
    }

    public static class CaseInsensitiveParameterNameHttpServletRequest extends HttpServletRequestWrapper {
        private final LinkedCaseInsensitiveMap<String[]> map = new LinkedCaseInsensitiveMap<>();

        @SuppressWarnings("unchecked")
        public CaseInsensitiveParameterNameHttpServletRequest(HttpServletRequest request) {
            super(request);
            map.putAll(request.getParameterMap());
        }

        @Override
        public String getParameter(String name) {

            String[] array = this.map.get(name);
            if (array != null && array.length > 0)
                return array[0];
            return null;
        }

        @Override
        public Map<String, String[]> getParameterMap() {
            return Collections.unmodifiableMap(this.map);
        }

        @Override
        public Enumeration<String> getParameterNames() {
            return Collections.enumeration(this.map.keySet());
        }

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

    }

}

定义Bean

    <!--输入参数忽略大小写-->
    <bean id="caseInsensitiveRequestFilterProxy" class="org.springframework.web.filter.DelegatingFilterProxy">
        <property name="targetBeanName" value="caseInsensitiveRequestFilter"/>
    </bean>
    <bean id="caseInsensitiveRequestFilter"
          class="com.hantianwei.util.CaseInsensitiveRequestParameterNameFilter">
    </bean>

web.xml 增加Filter

  <filter>
    <filter-name>caseInsensitiveRequestFilterProxy</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>caseInsensitiveRequestFilterProxy</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

到此再次运行项目就可以了,最上面的URL访问的是同一页面了

posted @ 2017-06-22 16:46  韩天伟  阅读(9326)  评论(2编辑  收藏  举报