views:

85

answers:

4

What is the preferred way to specify an HTTP "Location" Response Header in Spring MVC 3?

As far as I can tell, Spring will only provide a "Location" in response to a redirect ("redirect:xyz" or RedirectView), however there are scenarios where a Location should be sent along with the entity body (ex, as a result of a "201 Created").

I'm afraid my only option is manually specifying it:

httpServletResponse.setHeader("Location", "/x/y/z");

Is this correct? Is there a better way to tackle this problem?

A: 

That's the good way of doing it, and it conforms to the http specs.

rds
A: 

Yes, that is correct and standards-compliant.

Vanessa Williams
A: 

According to: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

Absolute URI should be used:

Location       = "Location" ":" absoluteURI  

And the URI should be escaped properly:

http://www.ietf.org/rfc/rfc2396.txt

tszming
A: 

Your approach seems fine, but to keep it clean you could put the code inside a custom HandlerInterceptor that only triggers when there's an HTTP 201, for example.

See here for more information.

James Earl Douglas