Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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 使用全局异常处理程序在SpringBoot应用程序中处理BindException_Java_Spring_Spring Boot_Spring Mvc_Exception - Fatal编程技术网

Java 使用全局异常处理程序在SpringBoot应用程序中处理BindException

Java 使用全局异常处理程序在SpringBoot应用程序中处理BindException,java,spring,spring-boot,spring-mvc,exception,Java,Spring,Spring Boot,Spring Mvc,Exception,我有一个带post请求的控制器。我试图用一个简单的NotNull注释验证POJO。我正在使用ControllerAdvice来处理异常 @PostMapping("/something") public MyResponse post(@Valid MyRequest request) { // nevermind... } 因此,我想为BindException创建自己的处理程序,但当我为BindException类创建ExceptionHandler时,spring应用程序不会启动。

我有一个带post请求的控制器。我试图用一个简单的NotNull注释验证POJO。我正在使用ControllerAdvice来处理异常

@PostMapping("/something")
public MyResponse post(@Valid MyRequest request) {
   // nevermind...
}
因此,我想为BindException创建自己的处理程序,但当我为BindException类创建ExceptionHandler时,spring应用程序不会启动。如果我注释掉handleBindException方法,应用程序将启动,如果发生BindException,它只返回400并注销错误,但没有任何内容作为响应主体发送回来


为BindExceptions创建自定义处理程序的解决方案是什么?

我发现问题是因为ResponseEntityExceptionHandler已经有了处理BindExceptions的方法。这意味着您不能覆盖该异常的异常处理。这也是许多异常的情况,请参见类ResponseEntityExceptionHandler:106。 因此,如果您想要创建自己的绑定异常处理程序,那么您需要重写在超类中处理它的方法。 看起来是这样的:

@Override
    protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers,
                                                         HttpStatus status, WebRequest request) {
    return handleExceptionInternal(...);
}
有了它,你可以归还任何你需要的东西。因此,我只找到了这个解决方案,如果有人知道其他解决方案,请毫不犹豫地写在这里:

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(value = BindException.class)
    protected ResponseEntity<Object> handleBindException(RuntimeException ex, WebRequest request) {
        return handleExceptionInternal(...);
    }
}
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerExceptionResolver]: Factory method 'handlerExceptionResolver' threw exception; nested exception is java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.validation.BindException]: {protected org.springframework.http.ResponseEntity com.liligo.sponsoredads.controller.RestResponseEntityExceptionHandler.handleBindException(java.lang.Exception,org.springframework.web.context.request.WebRequest), public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest) throws java.lang.Exception}
@Override
    protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers,
                                                         HttpStatus status, WebRequest request) {
    return handleExceptionInternal(...);
}