SpringBoot 請求參數(shù)忽略大小寫的實(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)
//重點(diǎn)
@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注解
補(bǔ)充:springboot 接受大寫參數(shù)時(shí),接收值為空的解決
入?yún)ⅲ?/h3>
{
"title":"文章標(biāo)題1",
"content":"文章內(nèi)容22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222",
"DOI":"123",
"PMID":"1234",
"email":"121607691@qq.com"
}
{
"title":"文章標(biāo)題1",
"content":"文章內(nèi)容22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222",
"DOI":"123",
"PMID":"1234",
"email":"121607691@qq.com"
}
springboot 接到的DOI和PMID 為null,頭字母改為小寫后正常。
原因及解決
是spring 使用@requestbody 接收時(shí)遵循駝峰命名規(guī)則,如果希望接收非駝峰的參數(shù)可以在對映的屬性上添加注解
@JsonProperty(value = "DOI") private String DOI;
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
- SpringBoot如何獲取Get請求參數(shù)詳解
- springboot如何設(shè)置請求參數(shù)長度和文件大小限制
- SpringBoot常見get/post請求參數(shù)處理、參數(shù)注解校驗(yàn)及參數(shù)自定義注解校驗(yàn)詳解
- SpringBoot之自定義Filter獲取請求參數(shù)與響應(yīng)結(jié)果案例詳解
- 使用SpringBoot請求參數(shù)過濾空格
- SpringBoot處理請求參數(shù)中包含特殊符號
- SpringBoot攔截器如何獲取http請求參數(shù)
- SpringBoot優(yōu)雅接收前端請求參數(shù)的詳細(xì)過程
相關(guān)文章
SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)示例詳解
這篇文章主要為大家介紹了SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解
這篇文章主要為大家介紹了Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
java使用鏈表實(shí)現(xiàn)約瑟夫環(huán)
這篇文章主要為大家詳細(xì)介紹了java使用鏈表實(shí)現(xiàn)約瑟夫環(huán),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
使用多個servlet時(shí)Spring security需要指明路由匹配策略問題
這篇文章主要介紹了使用多個servlet時(shí)Spring security需要指明路由匹配策略問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
springboot中的Application.properties常用配置
這篇文章主要介紹了springboot中的Application.properties常用配置,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
java基礎(chǔ)之TreeMap實(shí)現(xiàn)類全面詳解
這篇文章主要為大家介紹了java基礎(chǔ)之TreeMap實(shí)現(xiàn)類全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
MyBatis-Plus中靜態(tài)工具Db的多種用法及實(shí)例分析
本文將詳細(xì)講解MyBatis-Plus中靜態(tài)工具Db的各種用法,并結(jié)合具體案例進(jìn)行演示和說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
MyBatisPlus自定義JsonTypeHandler實(shí)現(xiàn)自動轉(zhuǎn)化JSON問題
這篇文章主要介紹了MyBatisPlus自定義JsonTypeHandler實(shí)現(xiàn)自動轉(zhuǎn)化JSON問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

