0%
需求
序列化忽略大小写
- 接口使用
@RequestBody
实现时忽略大小写序列化
- 配置三选一
application.yml
1 2 3 4
| spring: jackson: mapper: accept-case-insensitive-properties: true
|
JacksonConfig
1 2 3 4 5 6 7 8 9 10 11
| @Configuration public class JacksonConfig {
@Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return mapper; } }
|
WebMvcConfigurationSupport
1 2 3 4 5 6 7 8 9 10 11
| @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); MappingJackson2HttpMessageConverter json = new MappingJackson2HttpMessageConverter(mapper); converters.add(json); } }
|
URL 忽略大小写
WebMvcConfigurationSupport
1 2 3 4 5 6 7 8 9 10
| @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override public void configurePathMatch(PathMatchConfigurer configurer) { AntPathMatcher pathMatcher = new AntPathMatcher(); pathMatcher.setCaseSensitive(false); configurer.setPathMatcher(pathMatcher); } }
|
参数忽略大小写
OncePerRequestFilter
- 启动类添加
@ServletComponentScan
- 可以通过
urlPatterns
配置拦截路径
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
| @Order(1) @WebFilter(filterName = "caseInsensitiveFilter", urlPatterns = "/*") public class CaseInsensitiveRequestParameterNameFilter extends OncePerRequestFilter {
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { filterChain.doFilter(new CaseInsensitiveParameterNameHttpServletRequest(request), response); }
private static class CaseInsensitiveParameterNameHttpServletRequest extends HttpServletRequestWrapper { private final LinkedCaseInsensitiveMap<String[]> map = new LinkedCaseInsensitiveMap<>();
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); } } }
|
参考文献