Java处理无效请求映射url包括解码 [英] Java handle invalid request Mapping urls including decodings
问题描述
在我的 apiController 中,我有
in my apiController, I have
@Controller
@RequestMapping("api/v1/myservice")
@Slf4j
public class APIController {
@RequestMapping(value = "/validAPI1", method = RequestMethod.GET)
@ResponseBody
public String validAPI1() {
return "success";
}
}
我想捕获无效的传入 API 请求,例如 /api/v1/myservice/random124
,这可以通过在末尾添加一个方法来完成:
I want to catch invalid incoming API requests, such as /api/v1/myservice/random124
, and this can be done by adding a method at the end:
@RequestMapping(value = "/**", method = RequestMethod.GET)
@ResponseBody
public String handleInvalidAPIReq() {
return "watched and handled";
}
这有两个问题,没有在 URL 中捕获诸如 %
之类的特殊字符,并且没有处理输入的 URL.我试图反对的一个例子:/api/v1/myservice/%uff0e%uff0e%u2215%7bFILE%7d/abc12
This has 2 problems, does not catch special characters such as %
in URL, and does not take care of input URLs. An example I'm trying to fight against: /api/v1/myservice/%uff0e%uff0e%u2215%7bFILE%7d/abc12
请问我可以使用现有的库或方法来捕获无效的传入 API 请求吗?你能帮忙举个例子吗?欣赏吨.
Are there existing libraries or methods I can use to catch invalid incoming API requests please? Could you help with examples? Appreiate tons.
推荐答案
处理坏请求的常见方案是在 org.springframework.web.servlet.DispatcherServlet
中设置 throwExceptionIfNoHandlerFound
代码>类.您没有提到您编写的是哪种类型的应用程序,具有 spring-mvc 依赖项的简单战争存档或 Spring Boot 样式的应用程序.
The common solution for handling bad requests is to set throwExceptionIfNoHandlerFound
in org.springframework.web.servlet.DispatcherServlet
class. You didn't mentioned which type of application you wrote, simple war archive with spring-mvc dependency or spring boot styled application.
对于简单的 spring-mvc 应用程序,您可以在 web.xml 文件中设置该属性:
For simple spring-mvc application, you can set that property in web.xml file:
...
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...
对于 spring boot 应用程序,你只需要设置配置属性:
For spring boot application, you just need to set config property:
spring.mvc.throw-exception-if-no-handler-found=true
在这两种情况下,DispatcherServlet
都会抛出NoHandlerFoundException
异常,为了处理这个异常,需要添加ExceptionHandler
类如下::
In both cases, DispatcherServlet
will throw NoHandlerFoundException
exception, in order to handle this exception, you need to add the ExceptionHandler
class as follows::
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<String> handleNoHandlerFoundException(NoHandlerFoundException ex) {
return ResponseEntity.status(404).body("Error occurred");
}
}
注意: 当您启用throwExceptionIfNoHandlerFound
属性时,您应该记住静态资源,并为其添加自定义映射,否则将找不到资源.配置静态资源(Spring Boot):
Note: When you enable throwExceptionIfNoHandlerFound
property, you should remember about static resources, and add custom mappings for them, otherwise the resources will not be found.
Configure static resources (Spring Boot):
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/resources/static/
或者如果没有静态资源:
or if there are no static resources:
spring.resources.add-mappings=false
这篇关于Java处理无效请求映射url包括解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!