File name: twbs_jaxrs_contextobjects_uri.html
Obtaining information about URIs using UriInfo objects
Using Java API for
RESTful Web Services (JAX-RS), you can use the UriInfo object to access
request headers. The UriInfo object provides methods to enable you
to find or build URI information of a request.
About this task
Using an injected UriInfo object by the JAX-RS runtime
environment, the relative and absolute uniform resource identifier
(URI) information is known and available for modification. The @javax.ws.rs.core.Context
annotation indicates that a context object is injected. The javax.ws.rs.core.UriInfo
interface is the interface of the object that you want to inject.
You can use the UriInfo object to build absolute and relative URLs
using the UriBuilder class.
Procedure
- If a resource method signature can be modified, add the
@javax.ws.rs.core.Context javax.ws.rs.core.UriInfo parameter to the
method. When the resource method is invoked, the JAX-RS
runtime environment passes an object that implements the UriInfo object;
for example:
@Path("/contextexample")
public class RootResource {
@GET
public String getResource(@Context UriInfo uriInfo) {
/* This calls a method on the uriInfo object. */
return "The client used this URI to reach this resource method: " + uriinfo.getAbsolutePath().toASCIIString();
}
}
- If a resource method signature cannot be modified and the
class is a root resource, add the @javax.ws.rs.core.Context javax.ws.rs.core.HttpHeaders
field. When the resource is instantiated for a request,
an object that implements UriInfo is injected; for example:
@Path("/contextexample")
public class RootResource {
@Context
UriInfo uriInfo;
@GET
public String getResource() {
/* This calls a method on the uriInfo object. */
return "The client used this URI to reach this resource method: " + uriinfo.getAbsolutePath().toASCIIString();
}
}
Results
The resource method can use the URI and then build URI
information by using the UriInfo object
Example
In the following example, a list of books resource is
available at the following URL: http://<
hostname>:<
port>/<
context
root>/<
servlet path>/books. Suppose you need another
resource to reference the books resource. Instead of hardcoding the
URL into the application, use the UriInfo object to return a UriBuilder
class that dynamically builds the URI to resources in the application.
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.MediaType;
@Path ( "/books" )
public class BookListResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getBookList() {
return "a list of books";
}
}
import java.util.List;
import java.util.Locale;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.MediaType;
@Path ( "/allitems" )
public class AllItemsListResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getLocationOfAllItems(@Context UriInfo uriInfo) {
URI bookListURI = uriInfo.getBaseUriBuilder().path(BookListResource.class).build();
return Response.ok("The URI to the book list is: " + bookListURI..toASCIIString()).build();
}
}
In this information ...
| IBM Redbooks, demos, education, and more(Index)
Most of the following links will take you to information that is not part of the formal product documentation and is provided "as is." Some of these links go to non-IBM Web sites and are provided for your convenience only and do not in any manner serve as an endorsement by IBM of those Web sites, the material thereon, or the owner thereof.
|
|
