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:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javahonk</groupId>
	<artifactId>ConsumeRESTFulService</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>ConsumeRESTFulService</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.2.3.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>
	</dependencies>
</project>
  • 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:
<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.javahonk</groupId>
		<artifactId>ConsumeRESTFulService</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<groupId>com.javahonk</groupId>
	<artifactId>ConsumeRESTFulService</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>ConsumeRESTFulService</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<version>1.2.3.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.5.3</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.5.3</version>
		</dependency>


	</dependencies>
</project>
  • 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:
package com.javahonk;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Pivotalsoftware {

    private String name;
    private String about;
    private String phone;
    private String website;

    public String getName() {
        return name;
    }

    public String getAbout() {
        return about;
    }

    public String getPhone() {
        return phone;
    }

    public String getWebsite() {
        return website;
    }

}
  • 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:
package com.javahonk;

import java.util.HashMap;
import java.util.List;

import org.springframework.web.client.RestTemplate;

public class RESTFulServiceConsumer {

    public static void main(String args[]) {
        RestTemplate restTemplate = new RestTemplate();
        
        String consumeJSONString = restTemplate.getForObject("http://localhost:8080/SampleRESTFulService/consumeJSONString", String.class);
        System.out.println("JSON String: "+consumeJSONString);
        
        List<String> listStrings = restTemplate.getForObject("http://localhost:8080/SampleRESTFulService/consumeJSONList", List.class);
        System.out.println("List of String: "+listStrings);
        
        HashMap<String, String> hashMap = restTemplate.getForObject("http://localhost:8080/SampleRESTFulService/consumeJSONMap",  HashMap.class);        
        System.out.println("JSON Map: "+hashMap);
        
        
        Pivotalsoftware objectExample = restTemplate.getForObject("http://graph.facebook.com/pivotalsoftware", Pivotalsoftware.class);
        System.out.println("Name:    " + objectExample.getName());
        System.out.println("About:   " + objectExample.getAbout());
        System.out.println("Phone:   " + objectExample.getPhone());
        System.out.println("Website: " + objectExample.getWebsite());
    }

}

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 *