How to call one rest api from another rest api

In spring boot microservices architecture, all services are configured as rest services. There is a need for one rest service to be called by another rest service. A rest api service may depend on a number of other services. All spring boot downstream services need to be accessed from the main rest api. Spring boot supports calling one rest api from another rest api.

Spring boot supports calling one rest service to another rest service using the RestTemplate class. RestTemplate is a synchronised client side class that is responsible for calling another rest service. RestTemplate supports all HTTP methods such as GET, POST, DELET, PUT, HEAD, etc. The rest template facilitates the marshalling and unmarshalling of objects with JSON strings.



pom.xml

The pom.xml file is required to have a spring-boot-starter-web maven module. This module will start the default tomcat server in port 8080. The rest api service can be hosted on the tomcat server.

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
.............
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

..........
</project>


call one rest service from another rest service

In the example below, one rest service is called from another rest service. Student is a model class that contains student details. The api “/student” is the service invoked by another services. The rest apis “/getstudentstring” and “/getstudent” are invoking the rest service “/student” from the code.

TestController.java

package com.yawintutor;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {

	@GetMapping(value = "/student")
	public Student getStudent() {
		return new Student(1, "name1");
	}

	@GetMapping(value = "/getstudentstring")
	private String getStudentString()
	{
	    String uri = "http://localhost:8080/student";
	    RestTemplate restTemplate = new RestTemplate();
	    String result = restTemplate.getForObject(uri, String.class);
	    return result; 
	}

	@GetMapping(value = "/getstudent")
	private Student getStudentObject()
	{
	    String uri = "http://localhost:8080/student";
	    RestTemplate restTemplate = new RestTemplate();
	    Student result = restTemplate.getForObject(uri, Student.class);
	    return result; 
	}
}


Model class

The student.java is a model class used in the example. The student class is a java bean that contains two attributes id and name.

Student.java

package com.yawintutor;

public class Student {
	private int id;
	private String name;

	public Student() {
		
	}

	public Student(int id, String name) {
		setId(id);
		setName(name);
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}


Testing

The below api urls can be called either from post man or any browser. The url will return the student object in JSON format.

URL – http://localhost:8080/student

{"id":1,"name":"name1"}

URL – http://localhost:8080/getstudentstring

{"id":1,"name":"name1"}

URL – http://localhost:8080/getstudent

{"id":1,"name":"name1"}



Related Articles

Leave a Reply

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