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.
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"))); } }
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); } }
-- all users request --response: returning dummy user list-- user request by id --response: returning dummy user by id 20
--------------- 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
Dependencies and Technologies Used: