Rest annotations






@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;

@GET
Annotate your Get request methods with @GET.    This  annotation is use  for   read only operations . Means    whenever we need  get  data   from any  data source  then @GET annotation going to be used.
  Here is simple  example   of   how it is used .
      import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.core.Response;  
@Path("/hello")  


public class HelloService{  
    @GET  
    @Path("/{param}")  
    public Response getMsg(@PathParam("param") String msg) {  
        String output = "Mukesh" + msg;  
        return Response.status(200).entity(output).build();  
    }  
}  


@Produces
@Produces annotation specifies the type of output method (or web service) will produce.  The @produce   specify   what type of data format is available by the method.
    These are  following  type   data format   the


  1. @Produces("text/plain"): for downloading text file.
  2. @Produces("image/png"): for downloading png image file.
  3. @Produces("application/pdf"): for downloading PDF file.
  4. @Produces("application/vnd.ms-excel"): for downloading excel file.
  5. @Produces("application/msword"): for downloading ms word file.

1.  @Path("/files")  
public class FileDownloadService {  
    @GET  
    @Path("/txt")  


    @Produces("text/plain")  
    public Response getFile() {  
        // method  code    
    }  
 }  

2.
  @Path("/files")  
public class FileDownloadService {  
    @GET  
    @Path("/txt")  
    @Produces("image/png")  
    public Response getFile() {  
        // method  code    
    }  
 }  


 @Consumes
The @Consumes annotation is used to specify the MIME media types a REST resource can consume.

@Path("/files")  
public class FileUploadService {  
    @POST  
    @Path("/upload")  
    @Consumes(MediaType.MULTIPART_FORM_DATA)  
    public Response uploadFile(  ){
        // Method  code
     }
  }  
@path(“/empcnt”)
public class   Employee_Contact { 
@PUT
@Consumes("application/json")
@Produces("application/json")
@Path("/contactId")
public RestResponse<Contact> update(Contact contact) {
...
}

@Path
@Path annotation used to define  path URL   using   which   client   able to invoke the particular method.  After the @path annotation   we specify   the methods pathparamater.
@GET
@Produces("application/xml")
@Path("xml/{firstName}")
public Contact getXML() {


  // Method code
}
@PathParam
We can bind REST-style URL parameters to method arguments using @PathParam annotation as shown below.
@GET
@Produces("application/xml")
@Path("xml/{firstName}")
public Contact getXML(@PathParam("firstName") String firstName) {
  Contact contact = contactService.findByFirstName(firstName);
  return contac;
}

@GET
@Produces("application/json")
@Path("json/{firstName}")
public Contact getJSON(@PathParam("firstName") String firstName) {
  Contact contact = contactService.findByFirstName(firstName);
  return contact;
}
@QueryParam
Request parameters in query string can be accessed using @QueryParam annotation as shown below. This represent   the parameters  of the  query string of an URL
 @path(“/”)
 Public class  Company {
@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
Annotate POST request methods with @POST.   This usually   used to posting data in table. This  is not  idempotent .  This means  it  will repetitively add  data  for request. There may chance to add same data multiple time . Post  used to create data means new row in table.

import javax.ws.rs.FormParam;  
import javax.ws.rs.POST;  


import javax.ws.rs.Path;  
import javax.ws.rs.core.Response;  
@Path("/product")  
public class ProductService{  
    @POST  
    @Path("/add")  
    public Response addUser(  
        @FormParam("id"int id,  
        @FormParam("name") String name,  
        @FormParam("price"float price) {  
   
        return Response.status(200)  
            .entity(" Product added successfuly!<br> Id: "+id+"<br> Name: " + name+"<br> Price: "+price)  
            .build();  
    }  
}  
@PUT
Annotate  @PUT  annotation   is  update  the existing  row.   This usually   used to update  data in table. This  is  idempotent .  This means  it not repetitively add  data  for request.

import javax.ws.rs.FormParam;  
import javax.ws.rs.POST;  
import javax.ws.rs.Path;  
import javax.ws.rs.core.Response;  
@Path("/product")  
public class ProductService{  
    @PUT 
    @Path("/add")  


    public Response addUser(  
        @FormParam("id"int id,  
        @FormParam("name") String name,  
        @FormParam("price"float price) {  
   
        return Response.status(200)  
            .entity(" Product added successfuly!<br> Id: "+id+"<br> Name: " + name+"<br> Price: "+price)  
            .build();  
    }  
}  


@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
@DELETE
Annotate DELETE request methods with @DELETE.  This  annotation  used  when you want  to delete some  data or content  form your table ot text.

@DELETE
@Produces("application/json")
@Path("{contactId}")
public RestResponse<Contact> delete(@PathParam("contactId") int contactId) {
...
}











Rest annotations Rest    annotations Reviewed by Mukesh Jha on 11:06 PM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.