【发布时间】:2016-04-25 00:56:48
【问题描述】:
我想通过两种方式管理简单控制器或 RestController 抛出的异常: 1)html重定向 2) json错误
我测试了下面的代码:
@ControllerAdvice(annotations = Controller.class)
public class ExceptionHandlerController
{
@ExceptionHandler(Exception.class)
public ModelAndView handleException(HttpServletRequest _req, Exception _ex)
{
K_LOGGER.info("test");
return new ModelAndView();
}
}
@ControllerAdvice(annotations = RestController.class)
public class ExceptionHandlerRestController
{
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(HttpServletRequest _req, Exception _ex)
{
return new ResponseEntity<>("test", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RestController
public class GreetingController
{
@RequestMapping("/greetingexception")
public Greeting greetingException(@RequestParam(value = "name", defaultValue = "World") String name)
throws Exception
{
throw new Exception();
}
}
它不能正常工作,我总是通过ExceptionHandlerController而不是ExceptionHandlerRestController。
我认为这是因为@RestController 继承了@Controller。
您还有其他解决方案吗?
【问题讨论】:
标签: java ajax spring-mvc spring-boot