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

后端 未结 2 1813
有刺的猬
有刺的猬 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 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 :-(

提交回复
热议问题