今天项目上出现一个问题,是前端的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