JAX-RS HTTP Method Annotations

This article discusses about @GET, @PUT, @POST, @DELETE and @HEAD annotations defined by JAX-RS for corresponding HTTP methods.

JAX-RS HTTP Method Annotations

 
JAX-RS provides the following annotations to map to corresponding HTTP operations.

 

@GET

 
HTTP GET method retrieves information from a resource.

GET method calls are handled by JAX-RS methods annotated with @GET annotation.
 

@Path("myresource")
public class MyResource {
 
    /**
     * Method handling HTTP GET requests.
     */
    
    @GET
    public String getMethod() {
      // code
    }
}

 

@PUT

 
The PUT method requests that the enclosed entity be stored under the supplied Request-URI.(RFC 2616)

HTTP PUT method calls are handled by methods annotated with @PUT annotation.
 

@Path("myresource")
public class MyResource {
 
    /**
     * Method handling HTTP PUT requests.
     */
    
    @PUT
    public String putMethod() {
      // code
    }
}

 

@POST

 
The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.(RFC 2616)

HTTP POST method calls are handled by methods annotated with @POST annotation.
 

@Path("myresource")
public class MyResource {
 
    /**
     * Method handling HTTP POST requests.
     */
    
    @POST
    public String postMethod() {
      // code
    }
}

 

Difference between POST and PUT

 
From RFC 2616,

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI.

The URI in a POST request identifies the resource that will handle the enclosed entity.

The URI in a PUT request identifies the entity enclosed with the request.
 

@DELETE

 
HTTP DELETE method deletes the specified resource.

DELETE method calls are handled by methods annotated with @DELETE annotation.
 

@Path("myresource")
public class MyResource {
 
    /**
     * Method handling HTTP DELETE requests.
     */
    
    @DELETE
    public String deleteMethod() {
      // code
    }
}

 

@HEAD

 
HTTP HEAD method is similar to GET, but it only returns header information (no Response body returned).

Head method calls are handled by methods annotated with @HEAD annotation.
 

@Path("myresource")
public class MyResource {
 
    /**
     * Method handling HTTP HEAD requests.
     */
    
    @HEAD
    public String headMethod() {
      // code
    }
}

 

Reference

 
https://jersey.java.net/documentation/latest/index.html

https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags