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 上完成。
Does anybody know why I cannot use @ResponseStatus(reason = "My message")
on an exception handler in spring MVC while still returning a @ResponseBody. What seems to happen is that if I use the reason
attribute
// 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());
}
The code for this example is over on github.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来这是来自
AnnotationMethodHandlerExceptionResolver
的以下代码的直接结果,这已在 SPR-8251:
It seems that this is a direct result of the following code from
AnnotationMethodHandlerExceptionResolver
This has been reported to Springsource in SPR-8251:
郑重声明,自 Spring 3.2 起,情况变得更糟,因为 AnnotationMethodHandlerExceptionResolver 已被 ResponseStatusExceptionResolver 取代,而且确实如此:
这值得报告错误。此外,@ResponseStatus 记录在
setStatus
并且设计不当。它应该被称为@ResponseError
。我最终为此创建了两个问题: SPR-11192 和 SPR-11193。
快一年过去了,我的两个问题仍然悬而未决。我不认为 Spring WebMVC 是一流的 REST 框架,恕我直言,WebMVC 是为人类而不是机器:-(
For the record, since Spring 3.2, this got even worse because the
AnnotationMethodHandlerExceptionResolver
has been replaced by theResponseStatusExceptionResolver
and it does:This is worth a bug report. Moreover, the
@ResponseStatus
is documented withsetStatus
and is ill-designed. It should have been called@ResponseError
.I have created two issues for this finally: SPR-11192 and SPR-11193.
Almost a year has passed and my two issues are still open. I do not consider Spring WebMVC as a first-class REST framework which it isn't imho, WebMVC is for humas and not machines :-(