Close

JAX-RS - Injecting UriInfo with @Context annotation

[Last Updated: Sep 5, 2017]

An instance of UriInfo can be injected as field or method parameter using the @Context annotation. UriInfo provides access to application and request URI information.

Example

A JAX-RS resource injecting UriInfo

@Path("/users")
public class UserResource {
  @Context
  UriInfo uriInfo;

  public UserResource() {
      printUriInfo("In constructor");
  }

  @GET
  public String getUsers() {
      printUriInfo("In getUsers() method");
      return "returning dummy user list";
  }

  @GET
  @Path("{id}")
  public String getUserById(@PathParam("id") String id) {
      printUriInfo("In getUserById() method");
      return "returning dummy user by id " + id;
  }

  private void printUriInfo(String msg) {
      System.out.println("---------------");
      System.out.println(msg);
      System.out.println("-- UriInfo --");
      if (uriInfo == null) {
          System.out.println("UriInfo: " + uriInfo);
          return;
      }
      System.out.println("absolutePath: " + uriInfo.getAbsolutePath());
      System.out.println("baseUri: " + uriInfo.getBaseUri());
      System.out.println("matchedResource: " + uriInfo.getMatchedResources());
      System.out.println("matchedUri: " + uriInfo.getMatchedURIs());
      System.out.println("path: " + uriInfo.getPath());
      System.out.println("pathParameters: " + uriInfo.getPathParameters());
      System.out.println("pathSegments: " + uriInfo.getPathSegments());
      System.out.println("queryParameters: " + uriInfo.getQueryParameters());
      System.out.println("requestUri: " + uriInfo.getRequestUri());
      System.out.println("relativize test: " + uriInfo.relativize(URI.create("test")));
      System.out.println("resolve test: " + uriInfo.resolve(URI.create("test")));
  }
}

The client

public class UserClient {
  public static void main(String[] args) throws Exception {
      allUsersRequest();
      userRequestById();
  }

  private static void allUsersRequest() {
      System.out.println("-- all users request --");
      Client client = ClientBuilder.newBuilder().build();
      WebTarget target =
              client.target("http://localhost:8080/users");

      String response = target.queryParam("maxLimit", 500)
                              .request()
                              .get(String.class);
      System.out.println("response: " + response);
  }

  private static void userRequestById() {
      System.out.println("-- user request by id --");
      Client client = ClientBuilder.newBuilder().build();
      WebTarget target =
              client.target("http://localhost:8080/users/20");

      String response = target.request().get(String.class);
      System.out.println("response: " + response);
  }
}

Output

-- all users request --
response: returning dummy user list
-- user request by id --
response: returning dummy user by id 20

Output on server console

---------------
In constructor
-- UriInfo --
UriInfo: null
---------------
In getUsers() method
-- UriInfo --
absolutePath: http://localhost:8080/users
baseUri: http://localhost:8080/
matchedResource: [com.logicbig.example.UserResource@1ef63d0a]
matchedUri: [users]
path: users
pathParameters: {}
pathSegments: [users]
queryParameters: {maxLimit=[500]}
requestUri: http://localhost:8080/users?maxLimit=500
relativize test: http://localhost:8080/test
resolve test: http://localhost:8080/test
---------------
In constructor
-- UriInfo --
UriInfo: null
---------------
In getUserById() method
-- UriInfo --
absolutePath: http://localhost:8080/users/20
baseUri: http://localhost:8080/
matchedResource: [com.logicbig.example.UserResource@412614f1]
matchedUri: [users/20, users]
path: users/20
pathParameters: {id=[20]}
pathSegments: [users, 20]
queryParameters: {}
requestUri: http://localhost:8080/users/20
relativize test: http://localhost:8080/test
resolve test: http://localhost:8080/test

Example Project

Dependencies and Technologies Used:

  • jersey-server 2.25.1: Jersey core server implementation.
  • jersey-container-servlet 2.25.1: Jersey core Servlet 3.x implementation.
  • JDK 1.8
  • Maven 3.3.9

@Context UriInfo Example Select All Download
  • injecting-uri-info
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • UserResource.java

    See Also