yegor256
yegor256

Reputation: 105063

how can I get URI of currently dispatched web resource, in JAX-RS?

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?

Upvotes: 4

Views: 665

Answers (1)

kschneid
kschneid

Reputation: 5694

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.

Upvotes: 4

Related Questions