Showing posts with label Restful Webservice. Show all posts
Showing posts with label Restful Webservice. Show all posts

Tuesday, October 4, 2011

EhCache Integration with Spring and Hibernate. Step by Step Tutorial

EhCache is a very popular open source caching framework widely used for Java based Caching.

Currently it’s owned by Terracotta.org and 2.5 beta releases is available for download.

In this article I just focused on EhCache Integration with Spring and Hibernate also I just explained few basic concepts which are required to understand Code. For detailed Caching Concepts Please visit http://ehcache.org/

Cache Manager, Caches and Elements are main entities of EhCache

EhCache consists of a CacheManager, which manages Caches. Caches contain Elements, which are essentially name value pairs. Caches are physically implemented either in-memory, or on disk.

Integration with Spring

EhCache integration with Spring is quite simple. You just need to define some properties in spring configuration xml file and its ready to use. Spring is using Annotation to integrate EhCache and by this we can add caching to any method results

EhCache Annotations for Spring is available via maven, simply add the following dependency to your pom.xml  

 
   
Next Step is to perform changes in your spring Configuration file. Below is my Spring configuration file which contains EhCache configurations



Important point to note here is that Cache Manager Name and its Configuration Location. We can use cacheManager in our code to store and retrieve information from Cache.

ehcache.xml file is configuration file for EhCache where we can define configuration details of EhCache. In my case here is content of my ehcache.xml file



Here it’s clear that we have created a cache with name Customer, I will use this Cache to store Customer details

Next step is to use it in your code. In my example I created a simple Spring based application where we have Customer Controller which is used to retrieve Customer Information

We have Customer Object with Customer Id, name and address details and related setter getter methods


Next step is to create Controller. Customer Controller is very simple. I am using Spring Restful Web services to retrieve Customer Information. In my Controller I have no logic related to EhCache. I just have two versions of getCustomer method (one with Annotation and one without Annotation), save Customer and Clear Cache method. Code is simple and self explanatory for any Spring MVC developer.




If you notice Controller is calling Customer Service. Customer Service is our main class and it’s used to get Customer Information. It can get information from Database using Hibernate or from any backend. Here I just provided basic implementation. Idea is to understand usage of EhCache
Following Service methods are used to retrieve and store information in Cache


     
Previously we have defined “cacheManager” object in our Spring.xml file and also we have defined a Cache with name “customer” in ehcache.xml file. Its time to use both these configurations
getCache method is use to retrieve Cache Object and we are storing new Element in this cache. Element requires key, value pair so I am using id filed as a key. Later on we can use same key to retrieve Customer Object from Cache.

Another great way of using EhCache is Spring Annotation

   

Add Cacheable Annotation to methods you would like to cache. In our case we are using it for getCustomer method. If we call getCustomer method with customerId 1, first time complete method will be called. If we will call this method again with same customerId then response will be returned from Cache.

To Clear Cache we can just use TriggerRemove Annotation

 

Complete Source Code is present at following location

https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B0YFdqXJcI3mY2ZiYzA4NWQtNmQ0ZS00ZWM0LTlkMzktMmM3YmJmZjUzNDEy&hl=en_US

Integration with Hibernate as a Second Level Cache

Hibernate uses different type of Caches

·         The first cache type is the session cache. The session cache caches object within the current session.
·         The second cache type is the query Cache. The query cache is responsible for caching queries and their results.
·         The third cache type is the second level cache. The second level cache is responsible for caching objects across sessions.
EhCache is used as second level cache. EhCache integration with Hibernate is quite simple as well. You just need to define some properties in persistence.xml file.
EhCache jars are available via maven, simply add the following dependency to your pom.xml  

 
   
Next step is to perform changes in persistence.xml  

 

 Put ehcache.xml at your classpath. It could be in classes or WEB-INF folder

 

In you Entity just add Annotations related to ehcache and thats it.

 

Complete Source Code is present at following location

https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B0YFdqXJcI3mMDJiMDU4MmUtZWFkYy00MTAxLWFjMmYtMWYyMGNhMWY2Mjdj&hl=en_US





Wednesday, September 28, 2011

Restful WebServices using Spring

In recent years, Representational State Transfer (REST) has emerged as a popular information-centric alternative to traditional SOAP-based web services. In Spring, REST support builds upon Spring MVC

The fundamentals of REST

Representational: REST resources can be represented in virtually any form, including XML, JavaScript Object Notation (JSON), or even HTML—whatever form best suits the consumer of those resources
State: when working with REST, we’re more concerned with the state of a resource than with the actions we can take against resources.
Transfer: REST involves transferring resource data, in some representational form, from one application to another.

REST is about transferring the state of resources—in whatever form is most appropriate from a server to a client (or vice versa).

Spring supports development of REST resources in the following ways:

  • Controllers can handle requests for all HTTP methods, including the four primary REST methods: GET, PUT, DELETE, and POST.
  • The new @PathVariable annotation enables controllers to handle requests for parameterized URLs (URLs that have variable input as part of their path).
  • The <form:form> JSP tag from Spring’s form-binding JSP tag library, along with the new HiddenHttpMethodFilter, make it possible to submit PUT and DELETE requests from HTML forms, even in browsers that don’t support those HTTP methods.
  • Resources can be represented in a variety of ways using Spring’s view and view resolvers, including new view implementations for rendering model data as XML, JSON, Atom, and RSS.
  • The representation best suited for the client can be chosen using the new ContentNegotiatingViewResolver
  • View-based rendering can be bypassed altogether using the new @ResponseBody annotation and various HttpMethodConverter implementations.
  • Similarly, the new @RequestBody annotation, along with HttpMethodConverter implementations, can convert inbound HTTP data into Java objects passed into a controller’s handler methods.

Spring MVC’s model for writing controller classes is extremely flexible. Almost any method with almost any signature can be annotated to handle a web request. But a side effect of such flexibility is that Spring MVC allows you to develop controllers that aren’t ideal in terms of RESTful resources. It’s too easy to write RESTless controllers.

Example of Spring MVC (Restless controller)

@Controller
@RequestMapping("/displaySpittle.htm")
public class DisplaySpittleController {

private final SpitterService spitterService;

@Inject
public DisplaySpittleController(SpitterService spitterService)
{
this.spitterService = spitterService;
}

@RequestMapping(method=RequestMethod.GET)
public String showSpittle(@RequestParam("id") long id, Model model)
{
 model.addAttribute(spitterService.getSpittleById(id));
 return "spittles/view";
}
 }

Take note of the @RequestMapping annotation at the class level. It says that this controller will handle requests for /displaySpittle.htm. That seems to imply that this controller is focused on the specific use case of displaying spittles (which is corroborated by the name of the class).

Nothing is terribly wrong with how DisplaySpittleController is written. But it isn’t a RESTful controller. It’s action-oriented and focused on a specific use case: displaying a Spittle object’s details in HTML form. Even the controller’s class name agrees.

Handling RESTful URLs URLs are one of the first things that most people think about when starting to work with REST.


is an example of Restless URL. This URL doesn’t locate or identify a resource. It demands that the server display a Spittle. The only part of the URL that identifies anything is the id query parameter. The base portion of the URL is verb-oriented. That is to say that it’s a RESTless URL


is an example of Restful URL. One thing that’s not clear about this URL is what it does. That’s because the URL doesn’t do any-thing. Rather, it identifies a resource. Instead of using a query parameter to identify the resource, the entire base URL identifies the resource. In fact, the new URL has no query parameters at all

To enable parameterized URL paths, Spring 3 introduced a new @PathVariable anno-tation. To see how this works, look at SpittleController, a new Spring MVC control- ler that takes a resource-oriented approach to handling requests for Spittles.

@Controller
@RequestMapping("/spittles")
public class SpittleController
{
private SpitterService spitterService;

@Inject
public SpittleController(SpitterService spitterService)
{
 this.spitterService = spitterService;
}

 @RequestMapping(value="/{id}", method=RequestMethod.GET)
 public String getSpittle(@PathVariable("id") long id, Model model)
{
 model.addAttribute(spitterService.getSpittleById(id));
return "spittles/view";
}
 }

You’re probably wondering about those weird curly-braces in the URL pattern. The part that says {id} is a placeholder through which variable data will be pass into the method. It corresponds to the @PathVariable annotation on the id method parameter.



Restful Code Example

Following is Code for a Customer Controller. In this Code I tried to implement all CUID methods for Customer Object

package com.playground.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.playground.entity.Customer;
import com.playground.services.CustomerService;
@Controller

public class CustomerController {
            @Autowired @Qualifier("CustomerService")
            CustomerService service;
           
            @RequestMapping(value= "/findcustomer/{customerId}/",method={RequestMethod.GET})
            @ResponseStatus(HttpStatus.OK)
            public @ResponseBody String findCustomer(@PathVariable String customerId){
                        Customer c=service.findCustomer(customerId);
                        return "Here is Customer:"+c.getName();
            }
           
            @RequestMapping(value= "/savecustomers",method={RequestMethod.POST})
            @ResponseStatus(HttpStatus.CREATED)
            public @ResponseBody  String saveCustomer(@RequestParam(value="name") String name, @RequestParam(value="address", required=false) String address, @RequestParam(value="telephone") String telephone){
                         Customer c=new Customer();
                         c.setName(name);
                         c.setAddress(address);
                         c.setTelephone(telephone);
                         boolean result=service.saveCustomer(c);
                         if(result)
                                                return "Save Operation Completed Sucessfully";
                                    else
                                                return "Save Operation is not Sucessfull";
            }
           
            @RequestMapping(value= "/updatecustomers/{customerId}/{name}/{address}/{telephone}",method={RequestMethod.PUT})
            @ResponseStatus(HttpStatus.ACCEPTED)
            public  @ResponseBody String updateCustomerDetails(@PathVariable String customerId,@PathVariable String name, @PathVariable String address, @PathVariable String telephone){
                       
                        System.out.println("Here is the Customer Details:"+customerId+":"+name+":"+address+":"+telephone);
                         Customer c=new Customer();
                         c.setCustomerId(customerId);
                         c.setName(name);
                         c.setAddress(address);
                         c.setTelephone(telephone);
                         boolean result=service.updateCustomerDetails(c);
                         if(result)
                                                return "Update Operation Completed Sucessfully";
                                    else
                                                return "Update Operation is not Sucessfull";
            }
           
            @RequestMapping(value= "/deletecustomers/{customerId}",method={RequestMethod.DELETE})
            @ResponseStatus(HttpStatus.ACCEPTED)
            public  @ResponseBody String deleteCustomer(@PathVariable String customerId){
                                    System.out.println("Here is the Customer Id:"+customerId);
                                    boolean result=service.deleteCustomer(customerId);
                                    if(result)
                                                return "Delete Operation Completed Sucessfully";
                                    else
                                                return "Delete Operation is not Sucessfull";
            }
           
            @RequestMapping(value= "/allcustomers/",method={RequestMethod.GET})
            @ResponseStatus(HttpStatus.OK)
            public  @ResponseBody String getAllCustomers(){
                        List result=service.getAllCustomers();
                        return "Here is the list";
            }
           
}


URL for Find Customer

URL to Get All Customers

For Save Customer I have created a HTML form
Normal browsers doesn’t support PUT and DELETE HTTP method so I have created AJAX form to perform update and delete Operations

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Restful PlayGround</title>
   <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
            $(document).ready(function() {
                  //if submit button is clicked
                  $('#submit').click(function () {         
                        var customerId= $('input[name=customerId]');
                        //start the ajax
                        $.ajax({
                              //this is the php file that processes the data and send mail
                              url: "/RestfulPlayGround/deletecustomers/"+customerId.val(),     
                              type: "DELETE",
                              success: function (html) {                     
                                    alert(html);                 
                              }          
                        });
                        return false;
                  });  
                  $('#update').click(function () {         
                        //Get the data from all the fields
                        var customerId= $('input[name=customerId]');
                        var name= $('input[name=name]');
                        var address= $('input[name=address]');
                        var telephone= $('input[name=telephone]');
                       
                        //start the ajax
                        $.ajax({
                              //this is the php file that processes the data and send mail
                              url: "/RestfulPlayGround/updatecustomers/"+customerId.val()+"/"+name.val()+"/"+address.val()+"/"+telephone.val(),  
                              type: "PUT",
                              success: function (html) {                     
                                    alert(html);                 
                              }          
                        });
                        return false;
                  });  

            });  
    </script>
</head>
<body>
<br>
Delete Customer

<form  method="post">
<br>
Customer Id:<input type="text" name="customerId"><br>
<input type="submit" value="Submit" id="submit"/>
</form>


<br>
Update Customer
<form method="post">
<br>
Customer Id:<input type="text" name="customerId"><br>
Name:<input type="text" name="name"><br>
Address:<input type="text" name="address"><br>
Telephone:<input type="text" name="telephone"><br>
<input type="submit" id="update" value="Update">
</form>
<br>
Save Customer
<form method="POST" action="/RestfulPlayGround/savecustomers">
<br>
Name:<input type="text" name="name"><br>
Address:<input type="text" name="address"><br>
Telephone:<input type="text" name="telephone"><br>
<input type="submit" value="Save" id="save">
</form>

</body>
</html>

Complete Application Source Code is Present at following location
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B0YFdqXJcI3mYzM5ZDg0OTQtOTI1MS00YjBiLThhNjQtMTU1MTFmOWRlYjc2&hl=en

Reference: Spring in Action 3