Friday, August 3, 2018

How to use Rest Template with Spring Boot

This is very simple demonstration which shows how to use Rest Template with Spring Boot. In this example I will create Java APIs using Spring Boot to call another REST API to GET, POST and PATCH data.
To make it done, I'm using another REST application to work as a service. You can clone it using following git repository. This can be any application which creates REST APIs, may be Java, PHP, Python or whatever.
This application will run on port 8090. Please read the ReadMe file for API details.
Now I am going to create another Spring Boot application to to call those APIs using Rest Template.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


    <description>Demo project for Spring Boot</description>

        <relativePath/> <!-- lookup parent from repository -->





import lombok.Data;

public class User implements Serializable {
    private String name;
    private String email;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

public class ApiController {

    private ApiService apiService;

    public List<User> getData(){
        return apiService.getInfo();

    public User postData(@RequestBody User user){
        return apiService.saveData(user);

    public User getPatchData(@RequestBody User user, @PathVariable Long id){
        return apiService.patchData(user, id);

    public void deleteData(@PathVariable Long id){


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

public class ApiService {

    private RestTemplateBuilder restTemplateBuilder;

    public List<User> getInfo() {

        String url = "http://localhost:8090/api/user/users";

        ResponseEntity<List<User>> result =
                        HttpMethod.GET, null, new ParameterizedTypeReference<List<User>>() {});

        List<User> userList = new ArrayList<>();

        return userList;

    public User saveData(User requestUser) {

        String url = "http://localhost:8090/api/user/addUser";

        ResponseEntity<User> result =, requestUser, User.class);

        User user = new User();

        return user;

    public User patchData(User requestUser, Long id) {

        String url = "http://localhost:8090/api/user/patchUser/" + id;

        Object result =, requestUser, User.class);

        User user = new User();

        if(result != null) {
            if (requestUser.getName() != null) {
                user.setName(((User) result).getName());
            if (requestUser.getEmail() != null) {
                user.setEmail(((User) result).getEmail());
        return user;

    public void deleteData(Long id) {

        String url = "http://localhost:8090/api/user/deleteUser/" + id;

        }catch (Exception e){

You can use following curl commands to GET, POST, PATCH and DELETE data. My application is running on port 8088, by default it will run on port 8080. You can change it by setting server.port=8088 in

Get user data

curl -XGET 'http://localhost:8088/api/v2/users/getData/'

Add new user

curl -XPOST -H "Content-type: application/json" -d '{
    "name": "testUser",
    "email": ""
}' 'http://localhost:8088/api/v2/users/postData/' 

Update a user

curl -XPATCH -H "Content-type: application/json" -d '{
    "email": ""

}' 'http://localhost:8088/api/v2/users/patchData/2'

Remove a user

curl -XDELETE 'http://localhost:8088/api/v2/users/deleteData/2'

You can download the complete project from here.