How to handle java.lang.IllegalArgumentException: Invalid character found in the request target in Spring Boot

huangapple go评论72阅读模式
英文:

How to handle java.lang.IllegalArgumentException: Invalid character found in the request target in Spring Boot

问题

http://localhost:8081/api/projects?modifiedAt=2023-02\27 给出了错误,因为它包含'\'字符。要捕捉这个错误,您可以创建一个异常处理程序,如下所示:

@ExceptionHandler(IllegalArgumentException.class)
private ResponseEntity<ApiResponseDTO> handleIllegalArgumentException(IllegalArgumentException ex){
    log.info("IllegalArgumentException {} ", ex);

    List<String> errors = List.of(ex.getMessage());

    ApiResponseDTO<?> serviceResponse = ApiResponseDTO.builder()
            .status(ERROR)
            .errors(errors)
            .httpStatus(HttpStatus.BAD_REQUEST)
            .timestamp(ZonedDateTime.now(ZoneId.of("Z")))
            .build();

    return new ResponseEntity<>(serviceResponse, HttpStatus.BAD_REQUEST);
}

这将捕获IllegalArgumentException并返回一个HTTP 400 Bad Request响应。请确保已正确配置异常处理程序,并且IllegalArgumentException确实抛出并且没有被其他异常处理程序捕获。

英文:

I am trying to validate my below Request URL.

http://localhost:8081/api/projects?modifiedAt=2023-02

This is giving an error because of the '\' character;

java.lang.IllegalArgumentException: Invalid character found in the request target [/api/projects?modifiedAt=2023-02 ]. The valid characters are defined in RFC 7230 and RFC 3986
	at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:494) ~[tomcat-embed-core-9.0.71.jar:9.0.71]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:271) ~[tomcat-embed-core-9.0.71.jar:9.0.71]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.71.jar:9.0.71]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:891) ~[tomcat-embed-core-9.0.71.jar:9.0.71]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1784) ~[tomcat-embed-core-9.0.71.jar:9.0.71]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.71.jar:9.0.71]
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.71.jar:9.0.71]
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.71.jar:9.0.71]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.71.jar:9.0.71]
	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

In order to catch this error I have created an Exception handler as below

@ExceptionHandler(IllegalArgumentException.class)
    private ResponseEntity&lt;ApiResponseDTO&gt; handleIllegalArgumentException(IllegalArgumentException ex){
        log.info(&quot;IllegalArgumentException {} &quot;, ex);

        List&lt;String&gt; errors = List.of(ex.getMessage());

        ApiResponseDTO&lt;?&gt; serviceResponse = ApiResponseDTO.builder()
                .status(ERROR)
                .errors(errors)
                .httpStatus(HttpStatus.BAD_REQUEST)
                .timestamp(ZonedDateTime.now(ZoneId.of(&quot;Z&quot;)))
                .build();

        return new ResponseEntity&lt;&gt;(serviceResponse, HttpStatus.NOT_FOUND);
    }

But this error is not hit.

Any idea how we can catch this error in Spring boot

答案1

得分: 1

我通过https://stackoverflow.com/questions/51703746/setting-relaxedquerychars-for-embedded-tomcat的帮助成功解决了这个问题。

这个链接也很有帮助
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#application-properties.server.server.tomcat.relaxed-query-chars

在application.properties文件中添加了以下属性:

server.tomcat.relaxed-path-chars=\\
server.tomcat.relaxed-query-chars=\\
英文:

I was able to get this issue fixed with the help of https://stackoverflow.com/questions/51703746/setting-relaxedquerychars-for-embedded-tomcat

This link was also helpful
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#application-properties.server.server.tomcat.relaxed-query-chars

Added below attributes to the application.properties files

server.tomcat.relaxed-path-chars=\\
server.tomcat.relaxed-query-chars=\\ 

huangapple
  • 本文由 发表于 2023年2月27日 19:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定