Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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 控制器实现接口时未调用Spring@ExceptionHandler_Java_Spring_Spring Mvc - Fatal编程技术网

Java 控制器实现接口时未调用Spring@ExceptionHandler

Java 控制器实现接口时未调用Spring@ExceptionHandler,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有一个实现接口的@Controller,但是抽象BaseController上的@ExceptionHandler方法不会执行 我已经调试过了,AnnotationMethodHandlerExceptionResolver似乎有一行代码试图找到带注释的方法 ExceptionHandler exceptionHandler = AnnotationUtils.findAnnotation(method, ExceptionHandler.class); 但它只传递接口上的方法,我

我有一个实现接口的
@Controller
,但是抽象
BaseController
上的
@ExceptionHandler
方法不会执行

我已经调试过了,
AnnotationMethodHandlerExceptionResolver
似乎有一行代码试图找到带注释的方法

ExceptionHandler exceptionHandler 
    = AnnotationUtils.findAnnotation(method, ExceptionHandler.class);
但它只传递接口上的方法,我无法将这些方法添加到此接口

春天就是这样

final Class<?> handlerType = ClassUtils.getUserClass(handler);

那么,当您得到类型为
ServiceException
的异常时,您的处理程序不会被调用?它只会弹出到
DispatcherServlet
?此外,
AnnotationMethodHandlerExceptionResolver
在3.2中被弃用。你确定这就是正在使用的吗?只是确保-你确实为这个
控制器
类/包启用了注释驱动/组件扫描?@SotiriosDelimanolis对这两个都是,我将用新的Spring异常处理程序进行测试,看看它是否有任何不同。你用异常处理程序和基本控制器解决了这个问题吗?
interface ControllerInterface {
   String doStuff();
}

@Controller
public class Controller extends BaseController implements ControllerInterface {

  public String doStuff() {
     throw new ServiceException();
  }

}

public class BaseController {

    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Error handleException(ServiceException exp) {

        return new Error(exp.getCode(), exp.getMessage());
    }
}