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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5637950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:01:55  来源:igfitidea点击:

Spring MVC: using @ResponseStatus(reason = '') on a @ResponseBody exception handler in tomcat

javaspringspring-mvc

提问by Gareth Davis

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 reasonattribute

有谁知道为什么我不能@ResponseStatus(reason = "My message")在 spring MVC 中的异常处理程序上使用,同时仍然返回@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());
}

The code for this exampleis over on github.

此示例代码已在 github 上结束。

采纳答案by Gareth Davis

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

似乎这是以下代码的直接结果 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:

这已在SPR-8251 中报告给 Springsource :

回答by Michael-O

For the record, since Spring 3.2, this got even worse because the AnnotationMethodHandlerExceptionResolverhas been replaced by the ResponseStatusExceptionResolverand it does:

根据记录,自 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();
}

This is worth a bug report. Moreover, the @ResponseStatusis documented with setStatusand is ill-designed. It should have been called @ResponseError.

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

I have created two issues for this finally: SPR-11192and SPR-11193.

我最终为此创建了两个问题:SPR-11192SPR-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, WebMVCis for humas and not machines :-(

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