Spring MVC:在 tomcat 中的 @ResponseBody 异常处理程序上使用 @ResponseStatus(reason = '')

发布于 2024-10-31 23:59:41 字数 1004 浏览 0 评论 0原文

有谁知道为什么我不能在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-11-07 23:59:41

看来这是来自 AnnotationMethodHandlerExceptionResolver 的以下代码的直接结果,

private ModelAndView getModelAndView(Method handlerMethod, Object returnValue, ServletWebRequest webRequest)
        throws Exception {

    ResponseStatus responseStatusAnn = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class);
    if (responseStatusAnn != null) {
        HttpStatus responseStatus = responseStatusAnn.value();
        String reason = responseStatusAnn.reason();
        if (!StringUtils.hasText(reason)) {
            // this doesn't commit the response
            webRequest.getResponse().setStatus(responseStatus.value());
        }
        else {
            // this commits the response such that any more calls to write to the 
            // response are ignored
            webRequest.getResponse().sendError(responseStatus.value(), reason);
        }
    }
    /// snip
}

这已在 SPR-8251

It seems that this is a direct result of the following code from AnnotationMethodHandlerExceptionResolver

private ModelAndView getModelAndView(Method handlerMethod, Object returnValue, ServletWebRequest webRequest)
        throws Exception {

    ResponseStatus responseStatusAnn = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class);
    if (responseStatusAnn != null) {
        HttpStatus responseStatus = responseStatusAnn.value();
        String reason = responseStatusAnn.reason();
        if (!StringUtils.hasText(reason)) {
            // this doesn't commit the response
            webRequest.getResponse().setStatus(responseStatus.value());
        }
        else {
            // this commits the response such that any more calls to write to the 
            // response are ignored
            webRequest.getResponse().sendError(responseStatus.value(), reason);
        }
    }
    /// snip
}

This has been reported to Springsource in SPR-8251:

晌融 2024-11-07 23:59:41

郑重声明,自 Spring 3.2 起,情况变得更糟,因为 AnnotationMethodHandlerExceptionResolver 已被 ResponseStatusExceptionResolver 取代,而且确实如此:

protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request,
  HttpServletResponse response, Object handler, Exception ex) throws Exception {

  int statusCode = responseStatus.value().value();
  String reason = responseStatus.reason();
  if (this.messageSource != null) {
    reason = this.messageSource.getMessage(reason, null, reason, LocaleContextHolder.getLocale());
  }
  if (!StringUtils.hasLength(reason)) {
    response.sendError(statusCode);
  }
  else {
    response.sendError(statusCode, reason);
  }
  return new ModelAndView();
}

这值得报告错误。此外,@ResponseStatus 记录在 setStatus 并且设计不当。它应该被称为@ResponseError

我最终为此创建了两个问题: SPR-11192SPR-11193

快一年过去了,我的两个问题仍然悬而未决。我不认为 Spring WebMVC 是一流的 REST 框架,恕我直言,WebMVC 是为人类而不是机器:-(

For the record, since Spring 3.2, this got even worse because the AnnotationMethodHandlerExceptionResolver has been replaced by the ResponseStatusExceptionResolver and it does:

protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request,
  HttpServletResponse response, Object handler, Exception ex) throws Exception {

  int statusCode = responseStatus.value().value();
  String reason = responseStatus.reason();
  if (this.messageSource != null) {
    reason = this.messageSource.getMessage(reason, null, reason, LocaleContextHolder.getLocale());
  }
  if (!StringUtils.hasLength(reason)) {
    response.sendError(statusCode);
  }
  else {
    response.sendError(statusCode, reason);
  }
  return new ModelAndView();
}

This is worth a bug report. Moreover, the @ResponseStatus is documented with setStatus 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 :-(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文