This content originally appeared on DEV Community and was authored by realNameHidden
Producer application
Dependencies
Spring Web and Lombok
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ProducerApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ProducerApp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
EmployeeController
package com.example.demo.Controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.example.demo.entity.Employee;
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@GetMapping("/find/{id}")
public ResponseEntity<Employee> getEmp(@PathVariable Integer id){
return new ResponseEntity<Employee>(new Employee(id,"sam"),HttpStatus.OK);
}
}
Employee
package com.example.demo.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private Integer id;
private String name;
}
application.properties
spring.application.name=ProducerApp
server.port=8081
Consumer App
Dependencies
Spring Web and lombok
Employee
package com.example.demo.entity;
public class Employee {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
}
TestRunner
package com.example.demo.runner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.example.demo.entity.Employee;
@Component
public class TestRunner implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
//define url
String url = "http://localhost:8081/employee/find/1";
//create instance of RestTemplate
RestTemplate rt = new RestTemplate();
//make http call
ResponseEntity<Employee> reponse = rt.getForEntity(url, Employee.class);
System.out.println(reponse.getBody());
}
}
This content originally appeared on DEV Community and was authored by realNameHidden
realNameHidden | Sciencx (2024-09-17T03:30:57+00:00) Spring Boot RestTemplate getForEntity method. Retrieved from https://www.scien.cx/2024/09/17/spring-boot-resttemplate-getforentity-method/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.