We use Dropwizard / Jersey to create a web service. A resource has a path, and a method has a subpath. When you return the created response (201), the path of the method that we receive is added to the location that we provide. When you return the status "OK" with the location (as I know), everything is fine, and the location returns the same as we provided it.
How can we return a location that is not a subpath of our method location?
In the example below: get to "http: // localhost / foo / bar" (created status) responds with the location "http: // localhost / foo / bar / wibble" (note / foo / bar ).
while going to "http: // localhost / foo / baz" (ok status) responds with the location "http: // localhost / wibble" we want.
import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.Response; import java.net.URI; @Path("/foo") public class FooResource { @POST @Path("/bar") public Response bar() { URI uriOfCreatedResource = URI.create("/wibble"); return Response.created(uriOfCreatedResource).build(); } @POST @Path("/baz") public Response baz() { URI uriOfCreatedResource = URI.create("/wibble"); return Response.ok().location(uriOfCreatedResource).build(); } }
source share