Java Reference
In-Depth Information
@QueryParam
(
"start"
)
int
int
start
,
@QueryParam
(
"size"
)
int
int
size
,
@QueryParam
(
"orderBy"
)
List
<
String
>
orderBy
) {
...
}
}
You must define the generic type the
List
will contain; otherwise, JAX-RS won't know
which objects to fill it with.
Conversion failures
If the JAX-RS provider fails to convert a string into the Java type specified, it is considered a
client error. If this failure happens during the processing of an injection for an
@Mat-
rixParam
,
@QueryParam
, or
@PathParam
, an error status of 404, “Not Found,” is sent back
to the client. If the failure happens with
@HeaderParam
or
@CookieParam
, an error response
code of 400, “Bad Request,” is sent.
@DefaultValue
In many types of JAX-RS services, you may have parameters that are optional. When a client
does not provide this optional information within the request, JAX-RS will, by default, inject
a null value for object types and a zero value for primitive types.
Many times, though, a null or zero value may not work as a default value for your injection.
To solve this problem, you can define your own default value for optional parameters by us-
ing the
@javax.ws.rs.DefaultValue
annotation.
For instance, let's look back again at the
@QueryParam
example given earlier in this chapter.
In that example, we wanted to pull down a set of customers from a customer database. We
used the
start
and
size
query parameters to specify the beginning index and the number of
customers desired. While we do want to control the amount of customers sent back as a re-
sponse, we do not want to require the client to send these query parameters when making a
request. We can use
@DefaultValue
to set a base index and dataset size:
import
import
java.util.List
java.util.List
;
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
@GET
@Produces
(
"application/xml"
)
public
public
String
getCustomers
(
@DefaultValue
(
"0"
)
@QueryParam
(
"start"
)
int
int
start
,
@DefaultValue
(
"10"
)
@QueryParam
(
"size"
)
int
int
size
) {