java线上运行异常:Error parsing HTTP request header

发布时间 2023-12-04 09:56:00作者: 牛奶配苦瓜

1.部署异常如下:

image

2.出现原因

这个问题的原因是高版本的tomcat中的新特性:就是严格按照 RFC 3986规范进行访问解析,而 RFC 3986规范定义了Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符(RFC3986中指定了以下字符为保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ])。而我们的系统在通过地址传参时,在url中传了一段json,传入的参数中有"{"不在RFC3986中的保留字段中,所以会报这个错。

简而言之就是说,高版本的tomcat更加规范,才会出现这种报错

3.解决办法

3.1降低Tomcat的版本(太麻烦,可能你也没有权限做这件事)
3.2 在springboot中添加配置
3.2.1 http请求配置处理
@SpringBootApplication 
public class IntelligentBackApplication{ 
    public static void main(String[] args) {             
      System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true"); 
      SpringApplication.run(IntelligentBackApplication.class, args); 
} }

3.2.2 webmvc配置路径处理

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setUrlDecode(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}

原文链接:https://blog.csdn.net/a754782427/article/details/126409965