12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 15 ■ RESTFUL WEB SERVICESMethods or the Uniform InterfaceYou’ve seen how the HTTP protocol works <strong>with</strong> its requests, responses, and action methods (GET, POST,PUT, etc.). JAX-RS defines these common HTTP methods using annotations: @GET, @POST, @PUT, @DELETE,@HEAD, and @OPTIONS. Only public methods may be exposed as resource methods. Listing 15-9 shows acustomer resource exposing CRUD methods. Note that the createCustomer and updateCustomer methodstake an InputStream as a parameter which represents the HTTP request body.Listing 15-9. A Customer Resource Exposing CRUD Operations@Path("/customers")public class CustomerResource {@GETpublic List getListOfCustomers() {// ...}@POST@Consumes(MediaType.APPLICATION_XML)public Response createCustomer(InputStream is) {// ...}@PUT@Path("{customerId}")@Consumes(MediaType.APPLICATION_XML)public Response updateCustomer(@PathParam("customerId") String customerId,InputStream is) {// ...}@DELETE@Path("{customerId}")public void deleteCustomer(@PathParam("customerId") String customerId) {// ...}}When a resource method is invoked, parameters annotated <strong>with</strong> one of the extractor annotationsseen previously are filled. The value of a nonannotated parameter (called an entity parameter) is mappedfrom the request entity body and converted by an entity provider.Methods may return void, Response, or another <strong>Java</strong> type. Response is used when additionalmetadata needs to be provided; for example, when you create a new customer, you might need to sendback the URI of this customer.473

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!