如何设计一个优秀的小程序开发平台?
977
2023-02-15
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
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
return Collections.unmodifiableMap(this.map);
}
@Override
public Enumeration
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 接收时遵http://循驼峰命名规则,如果希望接收非驼峰的参数可以在对映的属性上添加注解
@jsonProperty(value = "DOI")
private String DOI;
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~