tags:

views:

60

answers:

2

How can I get a full URI of the resource currently dispatched in JAX-RS? I'm trying to return a URI of newly created object, and need a prefix part of it, 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 prefix part?

A: 

You'll need access to the HTTPRequest object. Check out the HTTPRequest.getURL() method.

Derp Developer
+1  A: 

One approach would be to inject UriInfo:

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

At that point, you can either use info directly or get a UriBuilder from one of its get*Builder methods.

kschneid