RESTful Web Service - JAX-RS Annotations - Contents:
Annotation |
Package Detail/Import statement |
@GET |
import javax.ws.rs.GET; |
@Produces |
import javax.ws.rs.Produces; |
@Path |
import javax.ws.rs.Path; |
@PathParam |
import javax.ws.rs.PathParam; |
@QueryParam |
import javax.ws.rs.QueryParam; |
@POST |
import javax.ws.rs.POST; |
@Consumes |
import javax.ws.rs.Consumes; |
@FormParam |
import javax.ws.rs.FormParam; |
@PUT |
import javax.ws.rs.PUT; |
@DELETE |
import javax.ws.rs.DELETE; |
REST follows one-to-one mapping between create, read, update, and delete (CRUD) operations and HTTP methods.
· To create a resource on the server, use POST.
· To retrieve a resource, use GET.
· To change the state of a resource or to update it, use PUT.
· To remove or delete a resource, use DELETE.
@GETpublic String getHTML() { ... }
@GET @Produces("application/xml") public Contact getXML() { ... } @GET@Produces("application/json") public Contact getJSON() { ... }
@GET @Produces("application/xml") @Path("xml/{firstName}") public Contact getXML() { ... }
@GET @Produces("application/xml") @Path("xml/{firstName}") public Contact getXML(@PathParam("firstName") String firstName) { Contact contact = contactService.findByFirstName(firstName); return contact; } @GET @Produces("application/json") @Path("json/{firstName}") public Contact getJSON(@PathParam("firstName") String firstName) { Contact contact = contactService.findByFirstName(firstName); return contact; }
@GET@Produces("application/json") @Path("json/companyList") public CompanyList getJSON(@QueryParam("start") int start, @QueryParam("limit") int limit) { CompanyList list = new CompanyList(companyService.listCompanies(start, limit)); return list; }
@POST @Consumes("application/json") @Produces("application/json") public RestResponse<Contact> create(Contact contact) { ... }
@PUT @Consumes("application/json") @Produces("application/json") @Path("{contactId}") public RestResponse<Contact> update(Contact contact) { ... }
@POST public String save(@FormParam("firstName") String firstName,@FormParam("lastName") String lastName) { ... }
@PUT @Consumes("application/json") @Produces("application/json") @Path("{contactId}") public RestResponse<Contact> update(Contact contact) { ... }
@DELETE @Produces("application/json") @Path("{contactId}") public RestResponse<Contact> delete(@PathParam("contactId") int contactId) { ... }