springboot接收http請(qǐng)求,參數(shù)中+號(hào)變成空格
小插曲
+ 在執(zhí)行URLEncoder.encode(String,"UTF-8")編碼后會(huì)變成 %2B
+ 在執(zhí)行URLDecoder.decode(String,"UTF-8")編碼后會(huì)變成 空格
解決get請(qǐng)求中的問(wèn)題
1.如前端發(fā)送的URL中含有“+”那么使用springboot框架接受的參數(shù)中 “+”會(huì)變成 空格
此時(shí)的解決辦法是 URLEncoder.encode(參數(shù),"UTF-8")
解決post請(qǐng)求中的問(wèn)題
1.如前端發(fā)送的URL中含有“+”那么使用springboot框架接受的參數(shù)中 “+”不會(huì)變成 空格
此時(shí)直接使用該參數(shù)就可以
SpringBoot問(wèn)題筆記:http請(qǐng)求參數(shù)含有特殊符號(hào)[]
請(qǐng)求報(bào)錯(cuò),調(diào)試控制器函數(shù)沒(méi)有觸發(fā)。
懷疑是請(qǐng)求參數(shù)中含有字符 [ ]
由博客了解到是由于Tomcat的新版本中增加了一個(gè)新特性,嚴(yán)格按照 RFC 3986規(guī)范進(jìn)行訪問(wèn)解析,而 RFC 3986規(guī)范定義了Url中只允許包含英文字母(a-zA-Z)、數(shù)字(0-9)、-_.~4個(gè)特殊字符以及所有保留字符(RFC3986中指定了以下字符為保留字符:! * ' ( ) ; : @ & = + $ , / ? # [ ])。
解決方法:修改tomcat配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@SpringBootApplication @EnableAsync public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication. class , args); } //允許http請(qǐng)求含有字符[]{} @Bean public TomcatServletWebServerFactory tomcatServletWebServerFactory (){ // 修改內(nèi)置的 tomcat 容器配置 TomcatServletWebServerFactory tomcatServlet = new TomcatServletWebServerFactory(); tomcatServlet.addConnectorCustomizers( (TomcatConnectorCustomizer) connector -> connector.setProperty( "relaxedQueryChars" , "[]" ) ); return tomcatServlet ; } } |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/YangX1aoLei/article/details/80508167