Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-14796

ModelAndView's setStatus does not work for @ExceptionHandler methods

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 4.3 GA, 4.3.3
    • Fix Version/s: 4.3.4, 5.0 M3
    • Component/s: [Documentation], Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      I am working with Spring Framework 4.3.3

      About Spring MVC about handle exceptions I have the following about a @ControllerAdvice (note: not totally valid about logic):

      @ExceptionHandler(value={EntityNotPreviouslyPersistedException.class})
      public ModelAndView handleEntityNotPreviouslyPersistedException(
      	EntityNotPreviouslyPersistedException ex,
      	WebRequest request,
      	HttpServletRequest request_) {
      
      	logger.info("EntityNotPreviouslyPersistedException...");
              ...
      		
      	logger.info("HTTP method: {}", request_.getMethod());
      
      	HttpStatus httpStatus = null;
      
      	if(request_.getMethod().equals(HttpMethod.DELETE.toString()) || request_.getMethod().equals(HttpMethod.PUT.toString())){
      		httpStatus = HttpStatus.BAD_REQUEST;
      	}
      	else if(request_.getMethod().equals(HttpMethod.GET.toString())){
      		httpStatus = HttpStatus.NOT_FOUND;
      	}
      	else{
      		logger.warn("It should be not printed...");
      	}
      
      	logger.info("HTTP method: {} - HttpStatus: {}", request_.getMethod(), httpStatus.toString());
      
      	ModelAndView mav = new ModelAndView();
      	mav.setStatus(httpStatus);
      	mav.addObject("message", error);
      	mav.setViewName("redirect:/error"); //but was:<302>
      	//mav.setViewName("/error");// was:<200>
      	return mav;
      }
      

      I did realize that mav.setStatus(httpStatus) is executed but finally is ignored.

      • If I use mav.setViewName("redirect:/error"); the status 302 is applied
      • If I use mav.setViewName("/error"); the status 200 is applied
      • If I don't use or not declare mav.setViewName the status 200 is applied

      Even when has more sense the 302 status, it to show an error page with some message.
      I have two questions from here:

      (1) Why the setViewName overrides the HttpStatus value?
      (2) In what valid scenario the setStatus is mandatory to be used? I did a research on Google about to get some examples seeing in action the setStatus method and nothing. Of course the better approach is use a @RequestMapping or @GetMapping method that returns a String object (view name) and use Model object.

      I know it is trivial, but I am with this doubt.

      Note: If I put in the current Spring Reference Documentation (htmlsingle mode) the setStatus term, there are no results.

      Thanks by your understanding.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              dr_pompeii Manuel Jordan
              Last updater:
              Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                2 years, 29 weeks, 1 day ago