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 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);
mybatis和springmvc}
public static class CaseInsensitiveParameterNameHttpServletRequest extends HttpServletRequestWrapper {
private final LinkedCaseInsensitiveMap<String[]> map = new LinkedCaseInsensitiveMap<>();
public CaseInsensitiveParameterNameHttpServletRequest(HttpServletRequest request) {
super(request);
map.ParameterMap());
}
@Override
public String getParameter(String name) {
String[] array = (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() {
umeration(this.map.keySet());
}
@Override
public String[] getParameterValues(String name) {
return (name);
}
}
}
并在启动类上加⼊@ServletComponentScan注解
补充:springboot 接受⼤写参数时,接收值为空的解决
⼊参:
{
"title":"⽂章标题1",
"content":"⽂章内容22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222", "DOI":"123",
"PMID":"1234",
"email":"121607691@qq"
}
springboot 接到的DOI和PMID 为null,头字母改为⼩写后正常。
原因及解决
是spring 使⽤@requestbody 接收时遵循驼峰命名规则,如果希望接收⾮驼峰的参数可以在对映的属性上添加注解@JsonProperty(value = "DOI")
private String DOI;
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。如有错误或未考虑完全的地⽅,望不吝赐教。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论