REST Templates

  • Rest Templates are used to integrate and show Response from web services directly using an url.
  • Using REST Templates we can call a service from another service which is also a REST service.
  • getForObject() method is used to get response from called webservice
    • First Parameter is String URL
    • Second Parameter is type of Object which will be mapped to response
    • This method then returns the type of object as specified in second parameter.
  • Generally we have String as response type but if we have created Generic Classes which can be mapped to response type we can use them too.
Controller Class
 package com.springimplant.course.controller;  
 import java.math.BigInteger;  
 import org.springframework.web.bind.annotation.GetMapping;  
 import org.springframework.web.bind.annotation.PathVariable;  
 import org.springframework.web.bind.annotation.RestController;  
 import org.springframework.web.client.RestTemplate;  
 import com.springimplant.course.core.Course;  
 @RestController  
 public class CatalogController {  
      @GetMapping("/")  
      public String getCatalogHome() {  
           String courseAppMsg="";  
           String courseUrl="http://localhost:8080/";  
           RestTemplate restTemplate=new RestTemplate();  
           courseAppMsg=restTemplate.getForObject(courseUrl,String.class);  
           return("Welcome to Course Catalog "+courseAppMsg);  
      }  
      @GetMapping("/catalog")  
      public String getCatalog() {  
           String courses="";  
           String courseUrl="http://localhost:8080/courses";  
           RestTemplate restTemplate=new RestTemplate();  
           courses=restTemplate.getForObject(courseUrl,String.class);  
           return("Our Courses are "+courses);  
      }  
      @GetMapping("/catalogcourse/{id}")  
      public String getCatalogCourse(@PathVariable("id") BigInteger id) {  
           Course course=new Course();  
           String courseUrl="http://localhost:8080/"+id;  
           RestTemplate restTemplate=new RestTemplate();  
           course=restTemplate.getForObject(courseUrl,Course.class);  
           return("Our Course is "+ course.getCoursename()+" by "+course.getAuthor());  
      }  
 }  
Course Class
 package com.springimplant.course.core;  
 import java.math.BigInteger;  
 public class Course {  
 private BigInteger courseid;  
 private String coursename;  
 private String author;  
 public Course() {  
 }  
 public BigInteger getCourseid() {  
      return courseid;  
 }  
 public void setCourseid(BigInteger courseid) {  
      this.courseid = courseid;  
 }  
 public String getCoursename() {  
      return coursename;  
 }  
 public void setCoursename(String coursename) {  
      this.coursename = coursename;  
 }  
 public String getAuthor() {  
      return author;  
 }  
 public void setAuthor(String author) {  
      this.author = author;  
 }  
 }  
  • We see here that we have hard coded the url which is not good approach.
    • We can use micro-services instead of this which is a better approach.

No comments:

Post a Comment

Spring Boot

What is circular/cyclic dependency in spring boot? When two services are interdependent on each other, that is to start one service, we requ...