今天項目上出現一個問題,是前端的GET請求url中帶有路徑參數,這個參數中有/這個特殊字符,在postman的url中已經轉移成了%2F,後端用的是springboot,並無收到這個請求,直接返回了400的錯誤前端
聽說是tomcat默認是不支持轉義的,須要手動設置一下轉化,這個搜索tomcat的設置能夠找到,可是這個是springboot,有內置的tomcat,可是在yml中找不到相關的配置。spring
修改一下啓動類,加一個系統參數,重寫WebMvcConfigurerAdapter的configurePathMatch方法.apache
@SpringBootApplication public class Application extends WebMvcConfigurerAdapter { public static void main(String[] args) throws Exception {
// step2: System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true"); SpringApplication.run(Application.class, args); }
// step1: @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setUrlDecode(false); configurer.setUrlPathHelper(urlPathHelper); } }
在tomcat目錄 /conf/catalina.properties 文件末尾添加下列內容後端
org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true tomcat
重啓tomcat成功解決問題springboot
符號 | URL中轉義結果 | 轉義碼 |
+ | URL 中 + 號表示空格 | %2B |
空格 | URL中的空格能夠用+號或者編碼 | %20 |
/ | 分隔目錄和子目錄 | %2F |
? | 分隔實際的URL和參數 | %3F |
% | 指定特殊字符 | %25 |
# | 表示書籤 | %23 |
& | URL中指定的參數間的分隔符 | %26 |
= | URL中指定參數的值 | %3D |