Invalid character found in spring request target.

My application starts with java -jar with version 1.5.6.RELEASE from spring boot.
The contents of one of my queries has the symbol "{". When a server is sent to the server, the following exception occurs:

java.lang.IllegalArgumentException: Invalid character found in request target. Valid characters are defined in RFC 7230 and RFC 3986 at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine (Http11InputBuffer.java:472) in org.apache.coyote.http11.Http11Processor.service (Http11P68orjava. apache.coyote.AbstractProcessorLight.process (AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process (AbstractProtocol.java:868) at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun ( NioEndpoint.java:1455) in org.apache.tomcat.util.net.SocketProcessorBase.run (SocketProcessorBase.java:49) in java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142) in java.util.concurrent .ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:617) in org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run (TaskThread.java:61) in java.lang.Thread.run (Thread.java:745 )

Where is it wrong? How to fix it?

EDIT1:
My rest is this:

var jsonData = { id: $("#hiddenId").val(), permitNumber: $("#txtPermitNumber").val(), permitToDate: $("#txtPermitToDate").val() } document.location = restUrl + "/print?reportParams= " + JSON.stringify(jsonData); 
+9
source share
4 answers

you start the spring boot application as shown

 $ java -jar -Dtomcat.util.http.parser.HttpParser.requestTargetAllow=|{} demo-0.0.1-SNAPSHOT.jar 

or encode uri like this

 document.location = restUrl + "/print?reportParams= " + encodeURI(JSON.stringify(jsonData)); 
+7
source

According to https://tomcat.apache.org/tomcat-8.5-doc/config/systemprops.html , requestTargetAllow is deprecated. For me, the other solutions presented here did not work either. According to the Tomcat documentation, I found a way to set the relaxedQueryChars property:

 @Bean public ConfigurableServletWebServerFactory webServerFactory() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { @Override public void customize(Connector connector) { connector.setProperty("relaxedQueryChars", "|{}[]"); } }); return factory; } 
+6
source

Easy way to just add this code to your main class

System.setProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow", "{}");

+2
source

For SpringBoot 1.5.17.RELEASE. The following code worked for me.

 @Configuration public class ServerConfig { @Bean public EmbeddedServletContainerFactory webServerFactory() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { @Override public void customize(Connector connector) { connector.setProperty("relaxedQueryChars", "|{}[]"); } }); return factory; } } 
-1
source

Source: https://habr.com/ru/post/1271870/


All Articles