In a controller that returns a RedirectView or "redirect:someViewName" the @ResponseStatus annotation is ignored. RedirectView always returns a hard coded 303 (or 302) in spite of the given value in the @ResponseStatus annotation.
i.e.
@
@Controller
@RequestMapping(value = "/games")
public class GameController {
...
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus( value=HttpStatus.CREATED )
public View createGame( @ModelAttribute("gameForm") GameForm gameForm ) {
return getRedirectViewForCreate(); // "redirect:somelocation.html";
}
...
RedirectView.sendRedirect(...) could look something like this:
protected void sendRedirect(
HttpServletRequest request, HttpServletResponse response, String targetUrl, boolean http10Compatible)
throws IOException {
if (http10Compatible) {
// Always send status code 302.
response.sendRedirect(response.encodeRedirectURL(targetUrl));
}
else if ( getHttpStatusCode() == null ) {
// Correct HTTP status code is 303, in particular for POST requests.
response.setStatus(303);
response.setHeader("Location", response.encodeRedirectURL(targetUrl));
} else {
if ( !isHttpStatusCodePreviouslySet() ) { // whatever... main thing is do NOT set it if the @ResponseStatus sets the status code
response.setStatus( getHttpStatusCode() );
}
response.setHeader("Location", response.encodeRedirectURL(targetUrl));
}
}
Extending RedirectView works for me, but of course "redirect:something.html" would not work without also changing the class instantiated when "redirect:" is used. But again, as is, it does give unexpected behaviour.
Affects: 3.0 M4
Issue Links:
17789 Support @ResponseStatus with RedirectView
10141 Modify RedirectView to allow 301 Permanent Redirects
Flyin Wolf opened SPR-6144 and commented
In a controller that returns a RedirectView or "redirect:someViewName" the
@ResponseStatus
annotation is ignored. RedirectView always returns a hard coded 303 (or 302) in spite of the given value in the@ResponseStatus
annotation.i.e. @
Got:
Expected:
I notice this could also maybe solve - http://jira.springframework.org/browse/SPR-5468
RedirectView.sendRedirect(...) could look something like this:
Extending RedirectView works for me, but of course "redirect:something.html" would not work without also changing the class instantiated when "redirect:" is used. But again, as is, it does give unexpected behaviour.
Affects: 3.0 M4
Issue Links:
17789 Support
@ResponseStatus
with RedirectView10141 Modify RedirectView to allow 301 Permanent Redirects
Referenced from: commits https://github.com/spring-projects/spring-framework/commit/5b12503c477a99eaddb25710a8b3a88d00e1c552