SpringBoot 請求參數(shù)忽略大小寫的實例
我就廢話不多說了,大家還是直接看代碼吧~
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 接受大寫參數(shù)時,接收值為空的解決
入?yún)ⅲ?/h3>
{
"title":"文章標題1",
"content":"文章內容22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222",
"DOI":"123",
"PMID":"1234",
"email":"121607691@qq.com"
}
{
"title":"文章標題1",
"content":"文章內容22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222",
"DOI":"123",
"PMID":"1234",
"email":"121607691@qq.com"
}
springboot 接到的DOI和PMID 為null,頭字母改為小寫后正常。
原因及解決
是spring 使用@requestbody 接收時遵循駝峰命名規(guī)則,如果希望接收非駝峰的參數(shù)可以在對映的屬性上添加注解
@JsonProperty(value = "DOI") private String DOI;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
Classloader隔離技術在業(yè)務監(jiān)控中的應用詳解
這篇文章主要為大家介紹了Classloader隔離技術在業(yè)務監(jiān)控中的應用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
使用多個servlet時Spring security需要指明路由匹配策略問題
這篇文章主要介紹了使用多個servlet時Spring security需要指明路由匹配策略問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
springboot中的Application.properties常用配置
這篇文章主要介紹了springboot中的Application.properties常用配置,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
MyBatis-Plus中靜態(tài)工具Db的多種用法及實例分析
本文將詳細講解MyBatis-Plus中靜態(tài)工具Db的各種用法,并結合具體案例進行演示和說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
MyBatisPlus自定義JsonTypeHandler實現(xiàn)自動轉化JSON問題
這篇文章主要介紹了MyBatisPlus自定義JsonTypeHandler實現(xiàn)自動轉化JSON問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

