Thursday 7 March 2019

RESTful Web Service - JAX-RS Annotations

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.

AnnotationsPackage Details
@GETimport javax.ws.rs.GET;
@Producesimport javax.ws.rs.Produces;
@Pathimport javax.ws.rs.Path;
@PathParamimport javax.ws.rs.PathParam;
@QueryParamimport javax.ws.rs.QueryParam;
@POSTimport javax.ws.rs.POST;
@Consumesimport javax.ws.rs.Consumes;
@FormParamimport javax.ws.rs.FormParam;
@PUTimport javax.ws.rs.PUT;
@DELETEimport 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) {
...

}


Example Restful Webservice Project


No comments:

Post a Comment

Unity Top Download

Latest post

An Introduction to Hybris from basics

An Introduction to Hybris from basics:  -- ecommerce site and PCM(Product content Management) solutions. eg. croma website.  -- having sear...

Popular posts