Java Reference
In-Depth Information
public
public
void
void
delete
(
@PathParam
(
"id"
)
int
int
id
) {...}
}
Successful Responses
Successful HTTP response code numbers range from 200 to 399. For the
create()
and
getCustomer()
methods of our
CustomerResource
class, they will return a response code
of 200, “OK,” if the
Customer
object they are returning is not null. If the return value is null,
a successful response code of 204, “No Content,” is returned. The 204 response is not an er-
ror condition. It just tells the client that everything went OK, but that there is no message
body to look for in the response. If the JAX-RS resource method's return type is void, a re-
sponse code of 204, “No Content,” is returned. This is the case with our
update()
and
de-
lete()
methods.
The HTTP specification is pretty consistent for the PUT, POST, GET, and DELETE meth-
ods. If a successful HTTP response contains a message body, 200, “OK,” is the response
code. If the response doesn't contain a message body, 204, “No Content,” must be returned.
Error Responses
In our
CustomerResource
example, error responses are mostly driven by application code
throwing an exception. We will discuss this exception handling later in this chapter. There
are some default error conditions that we can talk about right now, though.
Standard HTTP error response code numbers range from 400 to 599. In our example, if a cli-
ent mistypes the request URI, for example, to
customers
, it will result in the server not find-
ing a JAX-RS resource method that can service the request. In this case, a 404, “Not Found,”
response code will be sent back to the client.
For our
getCustomer()
and
create()
methods, if the client requests a text/html response,
the JAX-RS implementation will automatically return a 406, “Not Acceptable,” response
code with no response body. This means that JAX-RS has a relative URI path that matches
the request, but doesn't have a JAX-RS resource method that can produce the client's desired
response media type. (
Chapter 9
talks in detail about how clients can request certain formats
from the server.)
If the client invokes an HTTP method on a valid URI to which no JAX-RS resource method
is bound, the JAX-RS runtime will send an error code of 405, “Method Not Allowed.” So, in
our example, if our client does a PUT, GET, or DELETE on the
/customers
URI, it will get
a 405 response because POST is the only supported method for that URI. The JAX-RS im-
plementation will also return an
Allow
response header back to the client that contains a list