How can I get the URI of the currently sent web resource in JAX-RS?

How can I get the full resource URI that is currently sent to JAX-RS? I am trying to return the URI of a newly created object and it needs its prefix part with host, port, etc.:

// @import-s skipped public class Factory { @POST public final Response create() { Integer id; // new object created and id is set return Response.created( URI.create(prefix + "/object/" + id) ).build(); } } 

Where can I get this part of prefix ?

+4
source share
1 answer

One approach is to enter UriInfo :

public final Response create(@Context UriInfo info) {...}

At this point, you can directly use info or get UriBuilder from one of your get*Builder methods.

+4
source

All Articles