Thursday, May 10, 2012

Java utility to invoke Restful web services using Spring RestTemplate


This is a utility/framework to invoke Restful web services. Internally it uses Spring RestTemplate to invoke web services. Apart from spring jars, it uses some external jars. If your application already has these jars included, then you do not need to include these again.

You can download the RestFramework.rar file containing all the resources from below link:
https://docs.google.com/open?id=0B8O-miA80x0gUTBlSHoxQ0paWGc

The zip file contains below files:
  1.   RestfulWS.jar – jar to be included in your application. This has all the classes of this utility.
  2.  RestfulWS-src.jar – jar containing source code
  3.  External lib – other external jars required for RestfulWS.jar. Include these, if not present in your application.
  4.  Test – folder having all the test classes. Refer this to know how to use this utility.
  5. Class diagram.jpg - class diagram

Class Diagram:



If you are not able to view this class diagram, then you can refer the class diagram.jpg file included in RestFramework.rar.
In above class diagram, you can see that there are couple of concrete classes as well as couple of abstract classes. The concrete classes you can instantiate directly and use it by passing url, http method, and other required parameters through constructor and invoker method. The abstract classes at the lower end of the class hierarchy are meant to be used as a template. You can extend it and pass the required parameters(url, httpmethod, etc) by overriding corresponding methods in your concrete class.
This utility supports json as well as xml based Rest web services. If your request/response involves some other kind of data(for eg- in some web services call you may get array of bytes as response), then you can extend any of the suitable class in this hierarchy and add message converters(marshaller/unmarsheller) for that.

Details of interfaces, abstract classes and concrete classes:

    1) RestfulWebServiceInterface<WI,WO>
This is the top most interface parameterized with:
WI - input class type for web service
WO – class type for web service response

It has only one method execute(), which can be used to invoke web service.

    2) GenericRestfulWSInvoker<WI,WO>
This is an abstract class which implements RestfulWebServiceInterface<WI,WO>. It has implemented invoke() method of the RestfulWebServiceInterface. It also has multiple protected methods, which is used by execute() method internally. These methods can be overriden in subclasses if required.

    3)    JsonBasedRestfulWSInvoker<WI,WO>
This is a concrete class which extends GenericRestfulWSInvoker<WI,WO>. It overrides some of the protected methods of GenericRestfulWSInvoker<WI,WO> which is supposed to behave differently if the request and response type of web service is json. This class can be instantiated and web service can be invoked directly using instance of this class.
Please refer Test2.java in Test folder to refer sample code to use this.



    4)   XMLBasedRestfulWSInvoker<WI,WO>
This is a concrete class which extends GenericRestfulWSInvoker<WI,WO>. It overrides some of the protected methods of GenericRestfulWSInvoker<WI,WO> which is supposed to behave differently if the request and response type of web service is xml. This class can be instantiated and web service can be invoked directly using instance of this class.
Please refer Test1.java in Test folder to refer sample code to use this.

    5)  RestfulWSInvokerTemplateInterface<I,O,WI,WO>
This interface provides more generic and extended way of invoking web service. This interface can be used if your web service layer is isolated from your application’s other layer and you don’t want to expose the VO’s of web service layer (for request and response) to be exposed to other layer.
For e.g.:

In above diagram you can see, we have separate business layer and web service layer. Business layer passes object of type I. Web service layer accepts instance of I and converts that into instance of WI. This WI can then be converted into request and send to invoke web service call. Again response will be mapped to an instance of WO, which then be converted into instance of O and sent back to business layer. In this scenario “Business layer” is completely unknown about WI, WO and other web service specific details. If you follow this template pattern, then you need to create one concrete class for each of the web service call which will contain all the details about that particular web service call.

This interface has only one method invoke() to invoke web service

    6)  GenericRestfulWSInvokerTemplate<I, O, WI, WO>
This abstract class implements RestfulWSInvokerTemplateInterface<I,O,WI,WO> interface and extends GenericRestfulWSInvoker<WI,WO>.  It provides implementation of invoke() method. It also defines some of the abstract methods required for this template.

    7)   JsonBasedRestfulWSInvokerTemplate<I,O,WI,WO>
This is an abstract class which extends GenericRestfulWSInvokerTemplate<I, O, WI, WO>.
It overrides some of the protected methods of GenericRestfulWSInvoker<WI,WO> which is supposed to behave differently if the request and response type of web service is json. This class can be extended to create the concrete template class and web service can be invoked directly using instance of the extended class.
Please refer Test4.java in Test folder to refer sample code to use this. It also has a template class JsonBasedRestfulTemplateImpl.java which is used by Test4.java.

    8)  XMLBasedRestfulWSInvokerTemplate<I,O,WI,WO>
This is an abstract class which extends GenericRestfulWSInvokerTemplate<I, O, WI, WO>.
It overrides some of the protected methods of GenericRestfulWSInvoker<WI,WO> which is supposed to behave differently if the request and response type of web service is xml. This class can be extended to create the concrete template class and web service can be invoked directly using instance of the extended class.
Please refer Test3.java in Test folder to refer sample code to use this. It also has a template class XMLBasedRestfulTemplateImpl.java which is used by Test3.java.





Please let me know your thoughts about this utility, that will help me to improve this to make it more generic.








No comments:

Post a Comment