这篇文章给大家介绍怎么在SpringBoot中忽略请求参数的大小写,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
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 接受大写参数时,接收值为空的解决
{ "title":"文章标题1", "content":"文章内容22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222", "DOI":"123", "PMID":"1234", "email":"121607691@qq.com" }
springboot 接到的DOI和PMID 为null,头字母改为小写后正常。
是spring 使用@requestbody 接收时遵循驼峰命名规则,如果希望接收非驼峰的参数可以在对映的属性上添加注解
@JsonProperty(value = "DOI") private String DOI;
关于怎么在SpringBoot中忽略请求参数的大小写就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。