代码之家  ›  专栏  ›  技术社区  ›  Willy

Spring MVC异常处理程序不能与非注释控制器一起使用

  •  0
  • Willy  · 技术社区  · 6 年前

    一个是使用@Controller,如下所示

    @Controller
    @RequestMapping(value = "/2")
    public class TestController
    {
    
        @RequestMapping(value = "/test2", method = { RequestMethod.GET })
        @ResponseBody
        public String request2 (HttpServletRequest req)
    
        {
            throw new RuntimeException();
    
        }
    

    @Component
    public class Test2Controller implements Controller
    {
        @Override
        public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response)
                throws Exception
        {
            throw new RuntimeException();
        }
    }
    

    这两种类型的控制器都在同一dispatcher-servlet.xml下,以前工作得很好。但是,当我想为所有控制器设置全局异常处理程序时。只有@Controller可以工作。是否有任何额外的配置使这两种类型的控制器与Spring MVC异常处理程序一起工作?

    @ControllerAdvice
    public class GlobalExceptionHandler
    {
    
        @ExceptionHandler({ Throwable.class })
        public ResponseEntity handleException (Throwable e)
        {
            e.printStackTrace();
        }
    ```
    
    
    0 回复  |  直到 6 年前