Sunday, 5 July 2015

4. JAX-RS WebService : Use of @UPDATE, @DELETE and @POST

In this tutorial, we will see how to update, delete and add new product. As we have seen previously, all these transactions needs to be mapped to HTTP methods. updating existing product will be mapped to "PUT" method of HTTP. Similar way, deleting product maps to "DELETE" method of HTTP and adding new product maps to "POST" method.


First, we will update our dao class to perform these transactions,

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 public final boolean updateProduct(final Product product) {
  boolean isUpdated = false;
  if (productMap.containsKey(product.getProductId())) {
   productMap.put(product.getProductId(), product);
   isUpdated = true;
  }
  return isUpdated;
 }

 public final boolean deleteProduct(final int productId) {
  boolean isDeleted = false;
  if (productMap.containsKey(productId)) {
   productMap.remove(productId);
   isDeleted = true;
  }
  return isDeleted;
 }
 
 public final Product addProduct(final Product product){
  product.setProductId(productMap.size() + 1);
  productMap.put(productMap.size() + 1, product);
  return product;
 }


Now, we will add "deleteProduct" method in our resource class as below,

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 @DELETE
 @Path("{productId}")
 @Produces(MediaType.TEXT_PLAIN)
 public final Response deleteProduct(@PathParam("productId") final int productId) {
  final ProductDao productDao = new ProductDao();
  final boolean isDeleted = productDao.deleteProduct(productId);
  if (isDeleted) {
   return Response.status(Status.OK)
     .header("RESOURCE_DELETED", "TRUE")
     .entity("Resource deleted successfully!").build();
  } else {
   return Response.status(Status.NOT_FOUND).header("RESOURCE_DELETED", "FALSE")
     .entity("Something went wrong!")
     .build();
  }

 }

Return type of this method is "Response". We build the response based on whether deletion of product is successful or not. HTTP status code is set in response, Custom HTTP response header "RESOURCE_DELETED" is set as well. Entity is set as well to give meaningful text to client.


Request: deletion request to delete product with productId = 3


Success Response returned with our custom response header set to 'TRUE'


Response message also returned successfully.



Similar way, @PUT request can be added to update product details

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
 @PUT
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.TEXT_PLAIN)
 public final Response updateProduct(final Product product) {
  final ProductDao productDao = new ProductDao();
  final boolean isUpdated = productDao.updateProduct(product);
  if (isUpdated) {
   return Response.status(Status.OK).header("RESOURCE_UPDATED", "TRUE")
     .entity("Resource updated successfully!").build();
  } else {
   return Response.status(Status.NOT_FOUND).header("RESOURCE_UPDATED", "FALSE")
     .entity("Something went wrong!")
     .build();
  }

 }

Request:


Body of request :

Response:


Similar way, to add new resource, HTTP POST method can be used as below,

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.TEXT_PLAIN)
  public final Response addProduct(final @Context UriInfo uriInfo, Product product) { 
   final ProductDao productDao = new ProductDao();
   product = productDao.addProduct(product);
   final URI loc = uriInfo.getAbsolutePathBuilder()
     .path("" + product.getProductId()).build();
   return Response.created(loc)
     .entity("Product Added Successfully").build();   
  }

In this tutorial, we have seen number of concepts including how to use @PUT, @POST, @DELELE HTTP methods, how to send message body in request, how to return Response object in response, how to set Status code in response, how to set custom response header. In "addProduct" method for @POST, we are also setting "Location" header. This is to return resource uri of new product added. So client knows the uri of new resource which will be used for future requests.

I hope this tutorial helped you to understand RESTful webservices better. In next tutorial, we will see how to handle faults in REST. Thanks for reading this and STAY TUNED!

No comments:

Post a Comment