今天项目上出现一个问题,是前端的GET请求url中带有路径参数,这个参数中有/这个特殊字符,前端已经转移成了%2F,后端用的是springboot,并没有收到这个请求,直接返回了400的错误
原因
据说是tomcat默认是不支持转义的,需要手动设置一下转化,这个搜索tomcat的设置可以找到,但是这个是springboot,有内置的tomcat,但是在yml中找不到相关的配置。
解决方式
修改一下启动类,加一个系统参数,重写WebMvcConfigurerAdapter的configurePathMatch方法
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) throws Exception {
System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
SpringApplication.run(Application.class, args);
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
解决方法原链接Encoded slash (%2F) with Spring RequestMapping path param gives HTTP 400
本文解决了一个在SpringBoot项目中遇到的问题:当前端GET请求URL中包含路径参数且该参数含有%2F时,后端接收不到请求并返回400错误。原因是Tomcat默认不支持%2F的转义。解决方案是在启动类中添加系统参数,重写WebMvcConfigurerAdapter的configurePathMatch方法,禁用URL解码。
8396

被折叠的 条评论
为什么被折叠?



