1. Overview

Using spring boot we can build micro service in Java. Microservices is a small part of an application which performs small task in an application. In microservice architecture application divided into small part or modules, those small modules are known as microservices. Spring boot provides ways to easily develop microservices in Java.

While developing microservices, Its always requires communicating between microservices. Data exposed by one microservices should be required to read from another microservices.  Spring boot also provides good solution for communicate between microservices using Rest API. RestTemplate is class using that easily communication between microservices is possible. Spring boot RestTemplate Example:

RestTemplateBuilder class is used to createRestTemplate class. RestTemplateBuilder bean automatically created by spring boot.

            @Autowired
            private RestTemplateBuilder restTemplate;

2. Example

2.1 RestTemplate Get method Example

 

RestTemplate Get method Example

RestTemplate Get method Example

 

   @Autowired
   private RestTemplateBuilder restTemplate;
   public java.util.List<Employee> getAllEmployee() {
       Employee[] employee = restTemplate.build()
                                         .getForObject("http://localhost:8080/getAllEmployee", Employee[].class);
       return Arrays.asList(employee);
   }

2.2 RestTemplate Get method with Parameter Example

RestTemplate Get method with Parameter Example

RestTemplate Get method with Parameter Example

@Autowired
   private RestTemplateBuilder restTemplate;
   public Employee getEmployee() {
       return restTemplate.build().getForObject("http://localhost:8080/getEmployee?id=1", Employee.class);
   }

2.3 RestTemplate Get method with Parameter in Path Example

RestTemplate Get method with Parameter in Path Example

RestTemplate Get method with Parameter in Path Example

@Autowired
   private RestTemplateBuilder restTemplate;
   public Employee getEmployeePath() {
       return restTemplate.build().getForObject("http://localhost:8080/getEmployee/{id}", Employee.class, 1);
   }

2.3 RestTemplate POST method example

RestTemplate POST method example

RestTemplate POST method example

@Autowired
   private RestTemplateBuilder restTemplate;
   public Employee getEmployeePost() {
       return restTemplate.build().postForObject("http://localhost:8080/getEmployeePost?id=1", null, Employee.class);
   }

2.4 RestTemplate Read headers

RestTemplate Read headers

RestTemplate Read headers

@Autowired
    private RestTemplateBuilder restTemplate;
    public Employee getHeaders() {
        ResponseEntity responseEntity = restTemplate.build()
                                                    .getForEntity("http://localhost:8080/getEmployee/{id}", Employee.class, 2);
        responseEntity.getHeaders().entrySet().forEach((k) -> {
            System.out.println(k.getKey());
            k.getValue().forEach(System.out::println);
        });
        return (Employee) responseEntity.getBody();
    }

3. References

 

Was this post helpful?

Leave a Reply

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