Java Reference
In-Depth Information
You can also resolve URIs with respect to the base URI of your JAX-RS deployment using
the
resolve()
method:
public
public
URI
resolve
(
URI uri
);
Invoking this method is the same as calling
uriInfo.getBaseURI().resolve(uri)
.
There are other interesting tidbits available for building your URIs. In
Chapter 4
, I talked
about the concept of subresource locators and subresources. Code running within a subre-
source can obtain partial URIs for each JAX-RS class and method that matches the incoming
requests. It can get this information from the following methods on
UriInfo
:
public
public interface
interface
UriInfo
UriInfo
{
...
public
public
List
<
String
>
getMatchedURIs
();
public
public
List
<
String
>
getMatchedURIs
(
boolean
boolean
decode
);
}
So, for example, let's reprint the subresource locator example in
Chapter 4
:
@Path
(
"/customers"
)
public
public class
class
CustomerDatabaseResource
CustomerDatabaseResource
{
@Path
(
"{database}-db"
)
public
public
CustomerResource
getDatabase
(
@PathParam
(
"database"
)
String db
) {
Map map
= ...;
// find the database based on the db parameter
return
return new
new
CustomerResource
(
map
);
}
}
CustomerDatabaseResource
is the subresource locator. Let's also reprint the subresource
public
public class
class
CustomerResource
CustomerResource
{
private
private
Map customerDB
;
public
public
CustomerResource
(
Map db
) {
this
this
.
customerDB
=
db
;
}
@GET
@Path
(
"{id}"
)
@Produces
(
"application/xml"
)
public
public
StreamingOutput
getCustomer
(
@PathParam
(
"id"
)
int
int
id
,
@Context
UriInfo uriInfo
) {
for
(
String uri
:
uriInfo
.
getMatchedURIs
()) {