java Spring MVC:在tomcat中的@ResponseBody异常处理程序上使用@ResponseStatus(reason='')
有人知道为什么我不能在spring MVC中的异常处理程序上使用@ResponseStatus(reason = "My message")
,同时仍然返回@ResponseBody。如果我使用reason
属性
// this exception handle works, the result is a 404 and the http body is the json serialised
// {"message", "the message"}
@ExceptionHandler
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public Map<String, String> notFoundHandler(NotFoundException e){
return Collections.singletonMap("message", e.getMessage());
}
// this doesn't... the response is a 404 and the status line reads 'Really really not found'
// but the body is actually the standard Tomcat 404 page
@ExceptionHandler
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Really really not found")
public Map<String, String> reallyNotFoundHandler(ReallyNotFoundException e){
return Collections.singletonMap("message", e.getMessage());
}
github上的code for this example已经结束了
# 1 楼答案
这似乎是来自
AnnotationMethodHandlerExceptionResolver
的以下代码的直接结果这已在SPR-8251向Springsource报告: