概述
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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.annotation.Order;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.filter.OncePerRequestFilter;
@Order(1)
//重点
@WebFilter(filterName = "caseInsensitiveFilter", urlPatterns = "/*")
public class CaseInsensitiveRequestParameterNameFilter extends OncePerRequestFilter {
public CaseInsensitiveRequestParameterNameFilter() {
System.out.println("CaseInsensitiveRequestParameterNameFilter.CaseInsensitiveRequestParameterNameFilter()");
}
@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<>();
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);
}
}
}
并在启动类上加入@ServletComponentScan注解
最后
以上就是孤独自行车为你收集整理的SpringBoot 请求参数忽略大小写的全部内容,希望文章能够帮你解决SpringBoot 请求参数忽略大小写所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复