RestTemplate in Spring Boot

In Spring Boot, RestTemplate is used to make synchronous HTTP requests to other services (GET, POST, PUT, DELETE). Although Spring recommends using WebClient for reactive applications, RestTemplate is still widely used in many projects. Here’s a clear example:


1. Add Dependencies

If you are using Maven, make sure you have Spring Web dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. Configure RestTemplate Bean

Create a @Bean so you can inject it anywhere:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

3. Use RestTemplate in a Service

Here’s an example of calling a public API (JSONPlaceholder) to fetch posts.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class PostService {

    @Autowired
    private RestTemplate restTemplate;

    private final String BASE_URL = "https://jsonplaceholder.typicode.com/posts";

    // GET all posts
    public String getPosts() {
        return restTemplate.getForObject(BASE_URL, String.class);
    }

    // GET a post by id
    public String getPostById(int id) {
        return restTemplate.getForObject(BASE_URL + "/" + id, String.class);
    }

    // POST a new post
    public String createPost(Post post) {
        return restTemplate.postForObject(BASE_URL, post, String.class);
    }
}

4. Create a Post Model

public class Post {
    private Integer userId;
    private String title;
    private String body;

    // getters and setters
    public Integer getUserId() { return userId; }
    public void setUserId(Integer userId) { this.userId = userId; }

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }

    public String getBody() { return body; }
    public void setBody(String body) { this.body = body; }
}

5. Controller to Test

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

@RestController
@RequestMapping("/api")
public class PostController {

    @Autowired
    private PostService postService;

    @GetMapping("/posts")
    public String getAllPosts() {
        return postService.getPosts();
    }

    @GetMapping("/posts/{id}")
    public String getPost(@PathVariable int id) {
        return postService.getPostById(id);
    }

    @PostMapping("/posts")
    public String createPost(@RequestBody Post post) {
        return postService.createPost(post);
    }
}

✅ Test it:

  • GET all posts: http://localhost:8080/api/posts
  • GET post by ID: http://localhost:8080/api/posts/1
  • POST new post: http://localhost:8080/api/posts with JSON body:
{
  "userId": 1,
  "title": "My New Post",
  "body": "This is the content of the post"
}

Leave a Reply