Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java处理无效的请求映射URL,包括解码_Java_Spring_Api_Exception_Request Mapping - Fatal编程技术网

Java处理无效的请求映射URL,包括解码

Java处理无效的请求映射URL,包括解码,java,spring,api,exception,request-mapping,Java,Spring,Api,Exception,Request Mapping,在我的apiController中,我有 @Controller @RequestMapping("api/v1/myservice") @Slf4j public class APIController { @RequestMapping(value = "/validAPI1", method = RequestMethod.GET) @ResponseBody public String validAPI1() {

在我的apiController中,我有

@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
,这可以通过在末尾添加一个方法来完成:

@RequestMapping(value = "/**", method = RequestMethod.GET)
@ResponseBody
public String handleInvalidAPIReq() {
    return "watched and handled";
}
这有两个问题,无法捕获URL中的特殊字符,例如
%
,并且无法处理输入URL。我试图反对的一个例子是:
/api/v1/myservice/%uff0e%uff0e%u2215%7bFILE%7d/abc12


是否有现有的库或方法可用于捕获无效的传入API请求?你能帮我举个例子吗?处理错误请求的常见解决方案是通过
org.springframework.web.servlet.DispatcherServlet
类中的ExceptionIfNoHandlerFound设置
。您没有提到您编写了哪种类型的应用程序,使用SpringMVC依赖项的简单war归档或SpringBoot样式的应用程序

对于简单的spring mvc应用程序,您可以在web.xml文件中设置该属性:

。。。
春天
org.springframework.web.servlet.DispatcherServlet
ThroweExceptionIfNoHandlerFound
真的
1.
...
对于spring引导应用程序,您只需设置config属性:

spring.mvc.if-nothandler-found抛出异常=true
在这两种情况下,
DispatcherServlet
将抛出
NoHandlerFoundException
异常,为了处理此异常,您需要添加
ExceptionHandler
类,如下所示:

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");
    }

}
或者,如果没有静态资源:

spring.resources.add映射=false

如果要在URL中传递
%
,请将其正确编码为
%25
什么是
无效的传入API请求
?非常感谢您的回复。我使用wrapps spring配置的服务,无法将配置设置为看不到web.xml。控制器建议非常有用。我试图设置上述代码,但即使使用了最通用的@ExceptionHAndler(Exception.class),也没有达到断点。我在尝试探索反应。请帮助分享更多想法。非常感谢。请澄清这句话:“我使用wrapps spring配置的服务”,不管您使用的是
DispatcherServlet
,否则
RequestMapping
注释将无法工作。定义
DispatcherServlet
的方法很少,其中一种是
web.xml
,从Servlet 3.0开始,
DispatcherServlet
可以通过实现或扩展类来编程配置:
WebAppInitializer
AbstractDispatcherservleInitializer
AbstractAnnotationConfigDispatcherServletInitializer
。请检查您的项目中是否配置了Switch way
DispatcherServlet
。您实现的
ExceptionHandler
(如上所述)不起作用,因为
DispatcherServlet
不会引发异常,只有一种方法可以处理未映射的URL,您应该找到定义了
DispatcherServlet
的位置,并添加引发异常的属性