title: Setting the (http)status code (to something other than 500) after an exception in Spring
body: <p>As the title implies I want to change the http status after an exception occured. 
  The short background is that to be able to show an error page I need to send a status code of 200 to the client. (On error status codes it dumps the data and shows a generic error popup) </p>
  
  <p>To test this I have:</p>
  
  <pre><code>    @RequestMapping(value= "/err")
      public String throwError()
      {
          throw new IllegalStateException();
      }
  </code></pre>
  
  <hr>
  
  <p>In web-xml:</p>
  
  <pre><code>    &lt;error-page&gt;
          &lt;error-code&gt;500&lt;/error-code&gt;
          &lt;location&gt;/errorPage&lt;/location&gt;
      &lt;/error-page&gt;
  </code></pre>
  
  <hr>
  
  <p>And the errorPage controller (gets called)</p>
  
  <pre><code>    @RequestMapping(value = "/errorPage", method = RequestMethod.GET)
      public String errorPage(HttpServletResponse response, Model model, Locale locale ) {
          int errorStatus = response.getStatus();
          response.reset();
          model.addAttribute("errorStatus", errorStatus);
          response.setStatus(HttpServletResponse.SC_ACCEPTED);
          LOGGER.debug("----------&gt;" + response.getStatus());
          return "errorPage";
      }
  </code></pre>
  
  <hr>
  
  <p>The catalina log output is 200 BUT the response in the client is:</p>
  
  <p>Failed to load resource: the server responded with a status of 500 (Internal Server Error)</p>
  
  <p>Somewhere Spring encodes the exception as a statuscode 500 (I suppose it is in the HandlerExceptionResolver chain?) and then my errorPage controller is called.
  Spring seems to remember that an exception was thrown and ignores (or recodes?) the status code to 500 after the controller returns. </p>
  
  <p>The confusing (or maybe not?) thing is that if I return a Response Entity from the errorPage controller and set the status code this will be honored. Eg:</p>
  
  <pre><code>    return new ResponseEntity(HttpStatus.ACCEPTED);
  </code></pre>
  
  <p>I suppose that this is not a bug but a feature and that Spring knows better than me and I should really change my way of implementing this whole thing. (!!?)</p>
  
  <p>I cannot return just a ResponseEntity since I need to render the error page. I suppose I could if I streched myself render the page in the controller and put it into the ResponseEntity to achieve what I want but it just seems ... wrong.</p>
  
  <p>There must be a way of controlling what status code is returned other than returning a ResponseEntity (and bypassing whatever mechanism is resetting it to 500). Or just telling the framework not to re encode the thrown exception as a status code 500.</p>
  
  <p>To be complete I have tried setting the statuscode in a HandlerExceptionResolver and returning a ModelAndView with the same disappointing result. (500)
  I have also tried @ControllerAdvice @ExceptionHandler .. same result..</p>
  
  <p>Any clues to what happens after returning from the controller (or HandlerExceptionResolver) that sets the status code and how I can disable it?
  Maybe it is some unknown setting in my configuration that causes this?</p>