在项目中使用的tomcat是apache-tomcat-8.0.41,在完成后台逻辑后,前台js发起GET请求,报400错误,
$http.get('/test/getInfo?source='+source)
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
有这个报错信息可以知道是参数不合法,但是source我在前台做了处理
source本身只是json格式,
{"entityName":"","componentEntityName":"ZZZ"}
我通过JSON.stringify将其转化为string格式
"{"entityName":"","componentEntityName":"ZZZ"}"
貌似这样之后,后台不是识别这个格式。
在网上查了一些资料,觉得这部分说的不错
This behavior is introduced in all major Tomcat releases: Tomcat 7.0.73, 8.0.39, 8.5.7 For a quick fix, you can downgrade to one of older versions or set requestTargetAllow option Based on changelog, those changes could affect this behavior: Tomcat 8.5.3: Ensure that requests with HTTP method names that are not tokens (as required by RFC 7231) are rejected with a 400 response Tomcat 8.5.7: Add additional checks for valid characters to the HTTP request line parsing so invalid request lines are rejected sooner.
downgrade to one of older versions or set requestTargetAllow option Based on changelog, those changes could affect this behavior: Tomcat 8.5.3: Ensure that requests with HTTP method names that are not tokens (as required by RFC 7231) are rejected with a 400 response Tomcat 8.5.7: Add additional checks for valid characters to the HTTP request line parsing so invalid request lines are rejected sooner.
大意就是说tomcat的一些版本会对请求url中的字符进行检查,不符合要求的会返回400错误
解决的方法是换成旧的版本或者设置
requestTargetAllow
后面将tomcat换成apache-tomcat-8.0.37,问题解决。