我正在尝试验证下面的请求URL。
http://localhost:8081/api/projects?modifiedAt=2023-02\27
由于“\”字符,这将导致错误;
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]
为了捕获这个错误,我创建了一个异常处理程序,如下所示
@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);
}
但这个错误没有命中。
知道我们如何在Sping Boot 中捕获此错误吗
1条答案
按热度按时间huwehgph1#
我能够在Setting 'relaxedQueryChars' for embedded Tomcat的帮助下修复这个问题
此链接也很有用https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#application-properties.server.server.tomcat.relaxed-query-chars
在www.example.com文件中添加了以下属性application.properties