Consume RESTFul Web Service Java Spring Boot
Many techniques and examples are available to consume RESTFul web service, probably you would have tried many but using Spring boot RestTemplate is one of terribly easiest technique I have ever used to consume RESTFul web service. In this demo you will see how to use it. Please follow steps below:
Below are sample data that we will be consumed :
- Return type String:
- Return type List:
- Return type Map:
- Convert return JSON data into java object using URL: http://graph.facebook.com/pivotalsoftware Please click URL to open and see its output.
For all above sample service if you want to run please download below sample project.
Download Project: SampleRESTFulService
Consume RESTFul web service:
- Create maven project as below:
- Note: Above pom.xml we are using spring boot parent to load all jar. If you are working on project where parent already defined then you can not add multiple parent in one pom.xml. In that case please use below pom.xml:
- To bind return data type to List, Map, String is one line code and to convert return JSON to java object will be done using Java class with handful of properties with matching getter/setter methods. As you see below on top of class annotation @JsonIgnoreProperties has been used, which is from Jackson processing library to indicate that properties not bound in the class will be ignored.
- To consume RESTFul web service RESTFulServiceConsumer.java class created, which will use RestTemplate to fetch the data from service will convert to corresponding data type:
Note: Before running main class you will have to download above Sample RESTFul service on any server or if you have any service already runnign change URL and data type accordingly.
- To run right click to RESTFulServiceConsumer.java –> Run As –> Java Application you will be see below consume data out put:
- For more information please visit Spring tutorial here
Download Project: ConsumeRESTFulService