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 method. For example:
@Path("/customers")publicclassCustomerService{@GET@Produces("application/xml")publicStringgetAllCustomers(){}}
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 /customers. 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:
packageBecome an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access