Spring boot

Spring boot resttemplate example

Configuring RestTemplate in Spring Boot

Spring Boot Rest Template is one of the popular way to call remote REST services from your application,

If you need to call remote REST services from your application, you can use the Spring Framework’s RestTemplate class.

Spring boot provides you RestTemplateBuilder instead of RestTemplate, Since RestTemplate needs lot of customization before using it application.

The auto-configured RestTemplateBuilder ensures that sensible HttpMessageConverters are applied to RestTemplate instances.

No separate starters required to be added. Spring Boot Rest Template library comes with web starter.

SpringRestTemplateApplication.java

package com.candidjava.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class SpringRestTemplateApplication {

 @Bean
 public RestTemplate restTemplate(RestTemplateBuilder builder) {
  return builder.build();
 }

 public static void main(String[] args) {
  SpringApplication.run(SpringRestTemplateApplication.class, args);
 }

}

TodosService.java

package com.candidjava.springboot.service;

import java.util.Arrays;
import java.util.List;

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

import com.candidjava.springboot.models.Todos;

@Service
public class TodosService {

 @Autowired
 private RestTemplate restTemplate;

 private static final String baseURL = "https://jsonplaceholder.typicode.com/todos";

 public List < Todos > getAllTodos() {
  Todos[] todos = restTemplate.getForObject(baseURL, Todos[].class);
  List < Todos > listOfTodos = Arrays.asList(todos);
  return listOfTodos;

 }

 public Todos getTodosById(String id) {
  Todos todo = restTemplate.getForObject(baseURL + "/" + id, Todos.class);
  return todo;
 }

}

TodosController.java

package com.candidjava.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.candidjava.springboot.models.Todos;
import com.candidjava.springboot.service.TodosService;

@RestController
@RequestMapping(value = "/todos")
public class TodosController {
 @Autowired
 TodosService service;

 @GetMapping("/getAll")
 public List<Todos> get() {
  return service.getAllTodos();
 }

 @GetMapping("/get/{id}")
 public Todos getById(@PathVariable("id") String id) {
  return service.getTodosById(id);
 }

}

Todos.java

package com.candidjava.springboot.models;
public class Todos {
 private String id;
 private String userId;
 private String title;
 private String completed;

 public Todos() {}

 public String getUserId() {
  return userId;
 }

 public void setUserId(String userId) {
  this.userId = userId;
 }

 public String getTitle() {
  return title;
 }

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

 public String getCompleted() {
  return completed;
 }

 public void setCompleted(String completed) {
  this.completed = completed;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

}

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
  <groupId>com.candidjava</groupId>
  <artifactId>Spring_Boot_RestTemplate</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Spring_Boot_RestTemplate Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
  <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>

  </dependencies>
  <build>
    <finalName>Spring_Boot_RestTemplate</finalName>
    <plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
  </build>
</project>

Download

Download source code from my github account Click here