springboot springboot Invalid character found in the request target 特殊字符传参报错

1.出现的原因:主要和出现了特殊字符

SpringBoot 2.0.0 以上都采用内置tomcat8.0以上版本,而tomcat8.0以上版本遵从RFC规范添加了对Url的特殊字符的限制,url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~四个特殊字符以及保留字符( ! * ’ ( ) ; : @ & = + $ , / ? # [ ] ) (26*2+10+4+18=84)这84个字符,请求中出现了{}大括号或者[],所以tomcat报错。

2.解决办法:

1.第一种办法;降低tomocat的版本(当然不推荐)
2.第二种办法在springboot中的启动类中加入下面:

@MapperScan("com.wcy.mapper")
@SpringBootApplication
public class Springboot07MyblogApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot07MyblogApplication.class, args);
    }

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "|{}[]\\"));
        return factory;
    }
}

你可能感兴趣的:(springboot,java)