Ce contenu n'est pas disponible dans la langue sélectionnée.
50.2. Working with the full request URI
Abstract
UriInfo object. The UriInfo interface provides functions for decomposing the URI in a number of ways. It can also provide the URI as a UriBuilder object that allows you to construct URIs to return to clients.
50.2.1. Injecting the URI information Copier lienLien copié sur presse-papiers!
Overview Copier lienLien copié sur presse-papiers!
UriInfo object is decorated with the @Context annotation, the URI context for the current request is injected into the UriInfo object.
Example Copier lienLien copié sur presse-papiers!
Example 50.1. Injecting the URI context into a class field
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Path;
...
@Path("/monstersforhire/")
public class MonsterService
{
@Context
UriInfo requestURI;
...
}
50.2.2. Working with the URI Copier lienLien copié sur presse-papiers!
Overview Copier lienLien copié sur presse-papiers!
UriInfo interface provides methods for accessing the parts of the URI:
- the base URI
- the resource path
- the full URI
Getting the Base URI Copier lienLien copié sur presse-papiers!
@Path annotations. For example if a service implementing the resource defined in Example 46.5, “Disabling URI decoding” were published to http://fusesource.org and a request was made on http://fusesource.org/montersforhire/nightstalker?12 the base URI would be http://fusesource.org.
Getting the path Copier lienLien copié sur presse-papiers!
rootPath— /monstersforhire/getterPath— /mostersforhire/nightstalkerTheGETrequest was made on /monstersforhire/nightstalker.putterPath— /mostersforhire/911ThePUTrequest was made on /monstersforhire/911.
Example 50.2. Getting a resource's path
@Path("/monstersforhire/")
public class MonsterService
{
@Context
UriInfo rootUri;
...
@GET
public List<Monster> getMonsters(@Context UriInfo getUri)
{
String rootPath = rootUri.getPath();
...
}
@GET
@Path("\{type}")
public Monster getMonster(@PathParam("type") String type,
@Context UriInfo getUri)
{
String getterPath = getUri.getPath();
...
}
@PUT
@Path("\{id}")
public void addMonster(@Encoded @PathParam("type") String type,
@Context UriInfo putUri)
{
String putterPath = putUri.getPath();
...
}
...
}
Getting the full request URI Copier lienLien copié sur presse-papiers!
getRequestUri() methods would return http://fusesource.org/montersforhire/nightstalker?12. The getAbsolutePath() method would return http://fusesource.org/montersforhire/nightstalker.
50.2.3. Getting the value of URI template variables Copier lienLien copié sur presse-papiers!
Overview Copier lienLien copié sur presse-papiers!
Methods for getting the path parameters Copier lienLien copié sur presse-papiers!
UriInfo interface provides two methods, shown in Example 50.3, “Methods for returning path parameters from the URI context”, that return a list of the path parameters.
Example 50.3. Methods for returning path parameters from the URI context
MultivaluedMap<java.lang.String, java.lang.String> getPathParameters();MultivaluedMap<java.lang.String, java.lang.String> getPathParameters(boolean decode);getPathParameters() method that does not take any parameters automatically decodes the path parameters. If you want to disable URI decoding use getPathParameters(false).
color and note.
Example Copier lienLien copié sur presse-papiers!
Example 50.4. Extracting path parameters from the URI context
import javax.ws.rs.Path;
import javax.ws.rs.Get;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.MultivaluedMap;
@Path("/monstersforhire/")
public class MonsterService
@GET
@Path("\{type}\{size}")
public Monster getMonster(@Context UriInfo uri)
{
MultivaluedMap paramMap = uri.getPathParameters();
String type = paramMap.getFirst("type");
String size = paramMap.getFirst("size");
}
}