【发布时间】:2016-03-02 05:58:51
【问题描述】:
@ExceptionHandler 在控制器中调用时会返回带有 MyResponse 对象的 200 响应,但是当从 ControllerAdvice 调用它时,它会返回带有通用 404 消息的 404。 我希望它返回带有 MyResponse 对象的 200 响应。
我的异常处理代码如下,在 Controller 和 ControllerAdvice 中。我在测试 ControllerAdvice 时在 Controller 中将其注释掉了。调试显示在控制器中注释掉后在 ControllerAdvice 中调用的方法
@ExceptionHandler(MyException.class) public MyResponse handleMyException(HttpServletRequest req, MyException e) { return new MyResponse(MyResponse.ERROR_CODE, e.getErrorCode(), e.getMessage(), "", null); }
下面是我如何定义我的 ControllerAdvice。
@EnableWebMvc @ControllerAdvice @ResponseStatus(value= HttpStatus.OK) public class ControllerAdviceExceptionHandler {
【问题讨论】:
标签: java spring spring-mvc exception-handling http-status-code-404