Spring Boot - RestTemplate

This guide walks you through the process of creating an application that produces RESTful web services and consumes a RESTful web service.
To demonstrates the functionality we need a web service first. Name it ‘REST Producer’, because it provides data for the ‘REST Consumer’

REST Producer

Controller Class

@RestController
public class Controller {
@Autowired
UserService service;

@GetMapping(value = "/getUser")
public ResponseEntity<Map<Integer, User>> listAllUsers() {
return new ResponseEntity<>(service.getUser(), HttpStatus.OK);
}

@PostMapping(value = "/deleteUser")
public ResponseEntity<Map<Integer, User>> deleteUser(@RequestBody Key key) {
return new ResponseEntity<>(service.deleteuser(key.getKey()), HttpStatus.OK);
}

Key-POJO Class

public class Key {
private Integer key;

public Integer getKey() {
return key;
}

public void setKey(Integer key) {
this.key = key;
}
}

User-POJO Class

public class User {
private String name;
private String mail;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMail() {
return mail;
}

public void setMail(String mail) {
this.mail = mail;
}

public User(String name, String mail) {
super();
this.name = name;
this.mail = mail;
}

public static Map<Integer, User> userFactory() {
User user1 = new User("sibin", "knowledgeFactory4upeoples@gmail.com");
User user2 = new User("knf", "knowledgeFactory4upeoples@gmail.com");
User user3 = new User("sibin2", "knowledgeFactory4upeoples@gmail.com");
User user4 = new User("knf2", "knowledgeFactory4upeoples@gmail.com");
Map<Integer, User> list = new HashMap<>();
list.put(9898, user1);
list.put(8456, user2);
list.put(1234, user3);
list.put(5678, user4);
return list;
}
}

Service Class(UserService)

@Service
public class UserService {
public Map<Integer, User> getUser() {
Map<Integer, User> list = User.userFactory();
return list;
}

public Map<Integer, User> deleteuser(Integer UserId) {
Map<Integer, User> list = User.userFactory();
list.remove(UserId);
return list;
}
}

Spring Boot(Main Class)

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

application.properties

server.port=9094


Rest Consumer

Controller

@RestController
public class Controller {
@Autowired
UserService service;

@GetMapping(value = "/restTemplateGetTest")
public ResponseEntity<Map<Integer, User>> listAllUsers() {
return new ResponseEntity<>(service.getUser(), HttpStatus.OK);
}

@PostMapping(value = "/restTemplatePostTest")
public ResponseEntity<String> deleteUser(@RequestBody Key key)
throws URISyntaxException {
return new ResponseEntity<>(service.deleteuser(key.getKey()),
HttpStatus.OK);
}
}

Key-POJO Class

public class Key {
private Integer key;

public Integer getKey() {
return key;
}

public void setKey(Integer key) {
this.key = key;
}
}

User-POJO Class

public class User {
private String name;
private String mail;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMail() {
return mail;
}

public void setMail(String mail) {
this.mail = mail;
}
}

User Service Class

@Service
public class UserService {
public Map<Integer, User> getUser() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Map<Integer, User>> response = restTemplate.
exchange("http://localhost:9094/getUser",
HttpMethod.GET, null, new ParameterizedTypeReference
<Map<Integer, User>>() {
});
Map<Integer, User> users = response.getBody();
return users;
}

public String deleteuser(Integer UserId) throws URISyntaxException {
RestTemplate restTemplate = new RestTemplate();
Key key = new Key();
key.setKey(UserId);
String url = "http://localhost:9094/deleteUser";
String commonResult = restTemplate.postForObject(url, key, String.class);
return commonResult;
}
}

Spring Boot (Main Class)

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

application.properties

server.port=9091

Test 

Start Rest Producer  and Rest Consumer 
$ mvn spring-boot: run
  
Case 1:GET Service Call

     http://localhost:9091/restTemplateGetTest

     Response-200:
{
1234:
{
name: "sibin2",
mail: "knowledgeFactory4upeoples@gmail.com"
},
5678:
{
name: "knf2",
mail: "knowledgeFactory4upeoples@gmail.com"
},
8456:
{
name: "knf",
mail: "knowledgeFactory4upeoples@gmail.com"
},
9898:
{
name: "sibin",
mail: "knowledgeFactory4upeoples@gmail.com"
}
}

Case 2:Post Service Call

http://localhost:9091/restTemplatePostTest

Body

{"key":8456} 
Response:200
{
    "1234": {
        "name": "sibin2",
        "mail": "knowledgeFactory4upeoples@gmail.com"
    },
    "5678": {
        "name": "knf2",
        "mail": "knowledgeFactory4upeoples@gmail.com"
    },
    "9898": {
        "name": "sibin",
        "mail": "knowledgeFactory4upeoples@gmail.com"
    }
}

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete