Consume RESTFul Web Service Java Spring Boot

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:

Consume RESTFul Web Service Java Spring Boot

  • Return type List:

Consume RESTFul Web Service Java Spring Boot

  • Return type Map:

Consume RESTFul Web Service Java Spring Boot

For all above sample service if you want to run please download below sample project.

download  Download Project: SampleRESTFulService

Consume RESTFul web service:

  • Create maven project as below:

Consume RESTFul Web Service Java Spring Boot

  • pom.xml:

  • 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.

 

  • Pivotalsoftware.java:

  • 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:

Consume RESTFul Web Service Java Spring Boot

  • For more information please visit Spring tutorial here

download  Download Project:  ConsumeRESTFulService

Leave a Reply

Your email address will not be published. Required fields are marked *