Java Reference
In-Depth Information
Chapter 4. HTTP Method and URI
Matching
Now that we have a foundation in JAX-RS, it's time to start looking into the details. In
Chapter 3 , you saw how we used the @GET , @PUT , @POST , and @DELETE annotations to bind
Java methods to a specific HTTP operation. You also saw how we used the @Path annotation
to bind a URI pattern to a Java method. While applying these annotations seems pretty
straightforward, there are some interesting attributes that we're going to examine within this
chapter.
Binding HTTP Methods
JAX-RS defines five annotations that map to specific HTTP operations:
@javax.ws.rs.GET
@javax.ws.rs.PUT
@javax.ws.rs.POST
@javax.ws.rs.DELETE
@javax.ws.rs.HEAD
In Chapter 3 , we used these annotations to bind HTTP GET requests to a specific Java meth-
od. For example:
@Path ( "/customers" )
public
public class
class CustomerService
CustomerService {
@GET
@Produces ( "application/xml" )
public
public String getAllCustomers () {
}
}
Here we have a simple method, getAllCustomers() . The @GET annotation instructs the
JAX-RS runtime that this Java method will process HTTP GET requests to the URI /cus-
tomers . You would use one of the other five annotations described earlier to bind to different
HTTP operations. One thing to note, though, is that you may only apply one HTTP method
annotation per Java method. A deployment error occurs if you apply more than one.
Beyond simple binding, there are some interesting things to note about the implementation of
these types of annotations. Let's take a look at @GET , for instance:
Search WWH ::




Custom Search