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":"文章標(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 接收時遵循駝峰命名規(guī)則,如果希望接收非駝峰的參數(shù)可以在對映的屬性上添加注解
@JsonProperty(value = "DOI") private String DOI;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Java實現(xiàn)短信驗證碼服務(wù)的完整代碼示例
這篇文章主要介紹了Java實現(xiàn)短信驗證碼服務(wù)的完整代碼示例,文中使用阿里云的短信服務(wù)進行應(yīng)用開發(fā)的流程,包括將屬性寫入application.yml配置文件,定義類并指定配置文件,注入實體類對象等等,需要的朋友可以參考下2024-09-09Java中數(shù)組轉(zhuǎn)List的三種方法與對比分析
這篇文章主要給大家介紹了關(guān)于Java中數(shù)組轉(zhuǎn)List的三種方法與對比分析的相關(guān)資料,分別介紹了最常見方式、數(shù)組轉(zhuǎn)為List后,支持增刪改查的方式以及通過集合工具類Collections.addAll()方法,需要的朋友可以參考下2018-07-07SpringBoot中Token登錄授權(quán)、續(xù)期和主動終止的方案流程分析
SpringBoot項目中,基于Token的登錄授權(quán)方案主要有兩種:利用Session/Cookie和JWT,Cookie/Session方案有狀態(tài),不適合分布式架構(gòu),而JWT雖無狀態(tài),但存在過期時間不可強制失效、一次性等缺點,本文介紹SpringBoot中Token登錄授權(quán)、續(xù)期和主動終止的方案,感興趣的朋友一起看看吧2024-09-09SpringBoot整合jasypt進行重要數(shù)據(jù)加密的操作代碼
Jasypt(Java?Simplified?Encryption)是一個專注于簡化Java加密操作的開源工具,它提供了一種簡單而強大的方式來處理數(shù)據(jù)的加密和解密,使開發(fā)者能夠輕松地保護應(yīng)用程序中的敏感信息,本文給大家介紹了SpringBoot整合jasypt進行重要數(shù)據(jù)加密,需要的朋友可以參考下2024-05-05