您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在SpringBoot中忽略請(qǐng)求參數(shù)的大小寫,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
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í),接收值為空的解決
{ "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;
關(guān)于怎么在SpringBoot中忽略請(qǐng)求參數(shù)的大小寫就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。