Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ResponseStatus annotation is ignored in an @Controller redirect (RedirectView) [SPR-6144] #10812

Closed
spring-projects-issues opened this issue Sep 22, 2009 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Sep 22, 2009

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.
@

@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";
  }
...

Got:

$ curl -i -d name="game1" http://localhost:8080/rook2/rest/games/form.html
HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Location: http://localhost:8080/rest/games/1.html
Content-Language: en-US
Content-Length: 0
Date: Wed, 23 Sep 2009 04:03:15 GMT

Expected:

$ curl -i -d name="game2" http://localhost:8080/rook2/rest/games/form.html
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: /rook2/rest/games/2.html
Content-Language: en-US
Content-Length: 0
Date: Wed, 23 Sep 2009 02:44:55 GMT

I notice this could also maybe solve - http://jira.springframework.org/browse/SPR-5468

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:

Referenced from: commits 5b12503

@spring-projects-issues
Copy link
Collaborator Author

Flyin Wolf commented

http://jira.springframework.org/browse/SPR-6008 (resolved) was also a problem where the annotation was ignored.

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Added formatting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants