Java Reference
In-Depth Information
Chapter 20. Examples for
Chapter 5
Chapter 5
showed you how to use JAX-RS annotations to inject specific information about
an HTTP request into your Java methods and fields. This chapter implements most of the in-
jection scenarios introduced in
Chapter 5
so that you can see these things in action.
Example ex05_1: Injecting URI Information
This example illustrates the injection annotations that are focused on pulling in information
from the incoming request URI. Specifically, it shows how to use
@PathParam
,
@Mat-
rixParam
, and
@QueryParam
. Parallel examples are also shown using
javax.ws.rs.core.UriInfo
to obtain the same data.
The Server Code
The first thing you should look at on the server side is
CarResource
. This class pulls the
with the
javax.ws.rs.core.PathSegment
class:
src/main/java/com/restfully/shop/services/CarResource.java
@Path
(
"/cars"
)
public
public class
class
CarResource
CarResource
{
public
public static
static enum
enum
Color
{
red
,
white
,
blue
,
black
}
@GET
@Path
(
"/matrix/{make}/{model}/{year}"
)
@Produces
(
"text/plain"
)
public
public
String
getFromMatrixParam
(
@PathParam
(
"make"
)
String make
,
@PathParam
(
"model"
)
PathSegment car
,
@MatrixParam
(
"color"
)
Color color
,
@PathParam
(
"year"
)
String year
)
{
return
return
"A "
+
color
+
" "
+
year
+
" "