Hi all,
For a long time, I've been responding to "Create" operations using an @POST
method and I have gone through these steps in preparing a response:
ResponseBuilder rb = Response.ok();
String pk = ... insert into db, get primary key back...
rb.status(201);
rb.header("Location", pk)
This was working fine for as long as I remember, but I notice that on a new
project created with the maven archetype for Java EE this week, the
location header is being expanded to a (broken) absolute form.
So, first, yes, I know that the HTTP specification calls for an absolute
URL for the location header, but everyone knows that's horribly
inconvenient for clustered servers, and unnecessary. Consequently, the
de-facto standard is to return a relative URI. The W3C even has this noted
that they expect to change this though of course committees are slow moving
beasts :)
What gives, when did this change? Why did it change, and what do I do to
get sensible behavior back?
To clarify a couple of points:
1) I'm not setting the header using a URI object, I'm using a String
2) The pre-pended "absolutization" path is incomplete and therefore broken.
Specifically, if I have responded to a path .<context-root>/customers, and
return a location header of 1234, I don't even get
<context-root>/customers/1234, I get <context-root>/1234, so even if this
was supposed to be helpful, it's wrong.
Hmmm, I realize that this all sounds a bit pompous and assertive. Sorry, it
wasn't meant to; while I'm pretty sure I'm looking at a problem, if there's
something wrong with my approach, please let's assume that I'm OK with that
possibility and didn't intend to write aggressively!
Cheers,
Simon
--
Simon Roberts
Certified Professional Photographer
http://dancingcloudphotography.com
(303) 249 3613