英文:
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<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
答案1
得分: 1
我通过https://stackoverflow.com/questions/51703746/setting-relaxedquerychars-for-embedded-tomcat的帮助成功解决了这个问题。
在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=\\
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论