0

I am trying to validate my below Request URL.

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

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

java.lang.IllegalArgumentException: Invalid character found in the request target [/api/projects?modifiedAt=2023-02\27 ]. 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<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.NOT_FOUND);
    }

But this error is not hit.

Any idea how we can catch this error in Spring boot

dariosicily
  • 4,239
  • 2
  • 11
  • 17
Shanka Somasiri
  • 581
  • 1
  • 8
  • 30

1 Answers1

0

I was able to get this issue fixed with the help of 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=\\ 
Shanka Somasiri
  • 581
  • 1
  • 8
  • 30