1. 程式人生 > >java lang IllegalArgumentException Invalid character found

java lang IllegalArgumentException Invalid character found

               

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

org.apache.coyote.http11.AbstractHttp11Processor process

資訊: 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

at org.apache.coyote.http11.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:283)

at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017

)

at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1524)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1480)

at java.util.concurrent.ThreadPoolExecutor.runWorker(

ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:745)

環境描述

spring MVC 使用 @RequestBody 接收JSON引數轉為實體物件

public void save(@RequestBody Scene scene, HttpServletRequest          request, HttpServletResponse response) throws Exception {    }  

錯誤原因:

當在瀏覽器中訪問時 URL中帶有特殊字元,如花括號冒號時,就會出現這個錯誤。

例如:http://localhost:8080/index.do?{id:123}

解決方法:

原因已經清楚了,但解決方法有多種:

1、去除URL中的特殊字元;

2、特殊字元是出現在後面跟的引數中,對引數進行 URL 編碼,可以使用 JavaScript 中的 encodeURIComponent() 函式。

3、使用 Post 方法提交資料

4、更換低版本的Tomcat來規避這種問題。

5、在 conf/catalina.properties 中最後新增一行:

org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true

重啟伺服器後,解決問題。

官方說明:

org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH

If this is true ‘%2F’ and ‘%5C’ will be permitted as path.delimiters.If not specified, the default value of false will be used.