REST api is used for create, read, update, and delete (CRUD) operations and HTTP methods.
1.POST - to create or update a resource on the server.
2.GET - to retrieve a resource.
3.PUT - to update a resource.
4.DELETE - to delete a resource.
Annotations | Package Details |
---|---|
@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; |
Annotate your Get request methods with @GET
@GET
public String getHTML() {
...
}
@Produces
@Produces annotation specifies the type of output this method (or web service) will produce.@GET
@Produces("application/xml")
public Contact getXML() {
...
}
@GET
@Produces("application/json")
public Contact getJSON() {
...
}
@Path
@Path annotation specify the URL path on which this method will be invoked.@GET
@Produces("application/xml")
@Path("xml/{firstName}")
public Contact getXML() {
...
}
@PathParam
We can bind REST-style URL parameters to method arguments using @PathParam annotation as shown below.@GET
@Produces("application/xml")
@Path("xml/{name}")
public Product getXML(@PathParam("name") String name) {
Product product = productService.findByFirstName(name);
return product ;
}
@GET
@Produces("application/json")
@Path("json/{name}")
public Product getJSON(@PathParam("name") String name) {
Product product = productService.findByFirstName(name);
return product ;
}
@QueryParam
Request parameters in query string can be accessed using @QueryParam annotation as shown below.@GET
@Produces("application/json")
@Path("json/productList")
public ProductList getProductList(@QueryParam("start") int start, @QueryParam("limit") int limit) {
ProductList list = new ProductList(productService.listCompanies(start, limit));
return list;
}
The example above returns a list of companies (with server side pagination) which can be displayed with rich clients implemented using Ext-js or jQuery. .
@POST
Annotate POST request methods with @POST.@POST
@Consumes("application/json")
@Produces("application/json")
public getProduct<Product> create(Product product) {
...
}
@Consumes
The @Consumes annotation is used to specify the MIME media types a REST resource can consume.@PUT
@Consumes("application/json")
@Produces("application/json")
@Path("{productId}")
public ProductResponse<Product> update(Product product) {
...
}
@FormParam
The REST resources will usually consume XML/JSON for the complete Entity Bean. Sometimes, you may want to read parameters sent in POST requests directly and you can do that using @FormParam annotation. GET Request query parameters can be accessed using @QueryParam annotation.@POST
public String save(@FormParam("name") String name,
@FormParam("id") String id) {
...
}
@PUT
Annotate PUT request methods with @PUT.@PUT
@Consumes("application/json")
@Produces("application/json")
@Path("{id}")
public Update<Product> update(Product product) {
...
}
@DELETE
Annotate DELETE request methods with @DELETE.@DELETE
@Produces("application/json")
@Path("{contactId}")
public RestResponse<Contact> delete(@PathParam("contactId") int contactId) {
...
}
No comments:
Post a Comment