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忽略大小写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
}

}

参数名称忽略大小写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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

1
2
3
4
5
6
7
<!--输入参数忽略大小写-->
<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

1
2
3
4
5
6
7
8
<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访问的是同一页面了