Tomcat 7 不能传参json 解决办法

以前项目一直使用tomcat6 可是由于某个缘由更换了tomcat 7 发现传递json参数时会出现html

Using the protocol="HTTP/1.1" connector (Coyote)

After upgrading a site to Tomcat 7.0.73 from 7.0.72 or from anything earlier, a url with an unencoded { or } (ie. http://my.com?filter={"search":"isvalid"} ), now returns a 400 error code and logs the following error message:

"INFO: Error parsing HTTP request header
 Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986"

 百度后,尝试了各类办法 ,都没有效果。好比 java

  

Tomcat7以上版本不兼容 url中传json字符串,可更换低版本Tomcat(不建议)apache

可经过JS方法【encodeURIComponent(param)】对参数进行url转码解决,以下:json

var myUrl = '[{"itemId":"2","skuId":""}]';
var myOtherUrl ="http://localhost:8080/cart/batchremove?data="+encodeURIComponent(myUrl);

后来看到一篇博客,建议你们参考 原文:https://blog.csdn.net/aerchi/article/details/77963622tomcat

根据rfc规范,url中不容许有 |,{,}等特殊字符,但在实际生产中仍是有些url有可能携带有这些字符,特别是|仍是较为常见的。在tomcat升级到7之后,对url字符的检查都变严格了,若是出现这类字符,tomcat将直接返回400状态码。url

    后来有人对此提出了异义,见:    https://bz.apache.org/bugzilla/show_bug.cgi?id=60594.net

    通过一番讨价还价,tomcat的开发人员增长一项设置,容许配置在url能够出现的特殊字符,但也仅限于|,{,}三种,见:http://tomcat.apache.org/tomcat-8.0-doc/config/systemprops.html#Othercode

    该项设置在如下版本的tomcat中有效:htm

    - 8.5.x for 8.5.12 onwardsblog

    - 8.0.x for 8.0.42 onwards

    - 7.0.x for 7.0.76 onwards

 

解决方法: 
找到config/catalina.properties 最后一行,去掉注释# 在最后加一个{}

#tomcat.util.http.parser.HttpParser.requestTargetAllow=|
改成:
tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
相关文章
相关标签/搜索