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

后端 未结 2 1812
有刺的猬
有刺的猬 2021-01-05 04:10

Does anybody know why I cannot use @ResponseStatus(reason = \"My message\") on an exception handler in spring MVC while still returning a @ResponseBody. What se

相关标签:
2条回答
  • 2021-01-05 04:45

    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:

    0 讨论(0)
  • 2021-01-05 05:08

    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 :-(

    0 讨论(0)
提交回复
热议问题