我正在使用 spring-webmvc : 3.2.3.RELEASE (及其相关依赖项)。
我有这个控制器:
@Controller
@RequestMapping("/home")
public class HomeController {
@Autowired
MappingJacksonHttpMessageConverter messageConverter;
@RequestMapping(method = RequestMethod.GET)
public String get() {
throw new RuntimeException("XXXXXX");
}
@ExceptionHandler(value = java.lang.RuntimeException.class)
@ResponseStatus(HttpStatus.CONFLICT)
public ModelAndView runtimeExceptionAndView(ServletWebRequest webRequest) throws Exception {
ModelAndView retVal = handleResponseBody("AASASAS", webRequest);
return retVal;
}
@SuppressWarnings({ "resource", "rawtypes", "unchecked" })
private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest) throws ServletException, IOException {
ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
messageConverter.write(body, MediaType.APPLICATION_JSON, outputMessage);
return new ModelAndView();
}
}
由于“/home”方法抛出了正在使用@ExceptionHandler 处理的 RuntimeException,所以当调用 get() 方法时,我期望得到 HttpStatus.CONFLICT,但相反,我得到的是 HttpStatus.OK。有人可以告诉我应该怎么做才能从带注释的异常处理程序中获取响应状态吗?