SpringBoot 請(qǐng)求參數(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);
}
}
}
并在啟動(dòng)類上加入@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ù)可以在對(duì)映的屬性上添加注解
@JsonProperty(value = "DOI") private String DOI;
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- SpringBoot如何獲取Get請(qǐng)求參數(shù)詳解
- springboot如何設(shè)置請(qǐng)求參數(shù)長度和文件大小限制
- SpringBoot常見get/post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)及參數(shù)自定義注解校驗(yàn)詳解
- SpringBoot之自定義Filter獲取請(qǐng)求參數(shù)與響應(yīng)結(jié)果案例詳解
- 使用SpringBoot請(qǐng)求參數(shù)過濾空格
- SpringBoot處理請(qǐng)求參數(shù)中包含特殊符號(hào)
- SpringBoot攔截器如何獲取http請(qǐng)求參數(shù)
- SpringBoot優(yōu)雅接收前端請(qǐng)求參數(shù)的詳細(xì)過程
相關(guān)文章
Java實(shí)現(xiàn)短信驗(yàn)證碼服務(wù)的完整代碼示例
這篇文章主要介紹了Java實(shí)現(xiàn)短信驗(yàn)證碼服務(wù)的完整代碼示例,文中使用阿里云的短信服務(wù)進(jìn)行應(yīng)用開發(fā)的流程,包括將屬性寫入application.yml配置文件,定義類并指定配置文件,注入實(shí)體類對(duì)象等等,需要的朋友可以參考下2024-09-09
Java中數(shù)組轉(zhuǎn)List的三種方法與對(duì)比分析
這篇文章主要給大家介紹了關(guān)于Java中數(shù)組轉(zhuǎn)List的三種方法與對(duì)比分析的相關(guān)資料,分別介紹了最常見方式、數(shù)組轉(zhuǎn)為List后,支持增刪改查的方式以及通過集合工具類Collections.addAll()方法,需要的朋友可以參考下2018-07-07
SpringBoot中Token登錄授權(quán)、續(xù)期和主動(dòng)終止的方案流程分析
SpringBoot項(xiàng)目中,基于Token的登錄授權(quán)方案主要有兩種:利用Session/Cookie和JWT,Cookie/Session方案有狀態(tài),不適合分布式架構(gòu),而JWT雖無狀態(tài),但存在過期時(shí)間不可強(qiáng)制失效、一次性等缺點(diǎn),本文介紹SpringBoot中Token登錄授權(quán)、續(xù)期和主動(dòng)終止的方案,感興趣的朋友一起看看吧2024-09-09
Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
SpringBoot整合jasypt進(jìn)行重要數(shù)據(jù)加密的操作代碼
Jasypt(Java?Simplified?Encryption)是一個(gè)專注于簡化Java加密操作的開源工具,它提供了一種簡單而強(qiáng)大的方式來處理數(shù)據(jù)的加密和解密,使開發(fā)者能夠輕松地保護(hù)應(yīng)用程序中的敏感信息,本文給大家介紹了SpringBoot整合jasypt進(jìn)行重要數(shù)據(jù)加密,需要的朋友可以參考下2024-05-05

