how to call put method using RestTemplate Spring Boot

For Explanation watch video

Producer App

Employee

package com.example.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {


This content originally appeared on DEV Community and was authored by realNameHidden

For Explanation watch video

Producer App

Image description

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;
}

EmployeeController

package com.example.demo.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
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 {

 @PutMapping("/update")
 public ResponseEntity<String> modifyOneEmp(@RequestBody Employee emp){
  return new ResponseEntity<String>("FROM UPDATE"+emp.toString(),HttpStatus.OK);
 }

}

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.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </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>

Consumer App
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>ConsumerApp</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>ConsumerApp</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.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </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>

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;
}

EmployeeTestRunner

package com.example.demo.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
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 EmployeeTestRunner implements CommandLineRunner{

 @Override
 public void run(String... args) throws Exception {

  //String url
  String url = "http://localhost:8081/employee/update";

  //use rest template obj
  RestTemplate rt = new RestTemplate();

  HttpHeaders headers = new HttpHeaders();

  headers.setContentType(MediaType.APPLICATION_JSON);

  String body = "{\"id\":101,\"name\":\"AA\"}";

  HttpEntity<String> requestEntity = new HttpEntity<String>(body,headers);

  //3.make http call
  ResponseEntity<String> response = rt.exchange(url, HttpMethod.PUT,requestEntity,String.class);

  //4.print the result
  System.out.println(response.getBody());
  System.out.println(response.getStatusCode());
  System.out.println(response.getStatusCodeValue());
 }

}


This content originally appeared on DEV Community and was authored by realNameHidden


Print Share Comment Cite Upload Translate Updates
APA

realNameHidden | Sciencx (2024-10-18T03:32:07+00:00) how to call put method using RestTemplate Spring Boot. Retrieved from https://www.scien.cx/2024/10/18/how-to-call-put-method-using-resttemplate-spring-boot/

MLA
" » how to call put method using RestTemplate Spring Boot." realNameHidden | Sciencx - Friday October 18, 2024, https://www.scien.cx/2024/10/18/how-to-call-put-method-using-resttemplate-spring-boot/
HARVARD
realNameHidden | Sciencx Friday October 18, 2024 » how to call put method using RestTemplate Spring Boot., viewed ,<https://www.scien.cx/2024/10/18/how-to-call-put-method-using-resttemplate-spring-boot/>
VANCOUVER
realNameHidden | Sciencx - » how to call put method using RestTemplate Spring Boot. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/18/how-to-call-put-method-using-resttemplate-spring-boot/
CHICAGO
" » how to call put method using RestTemplate Spring Boot." realNameHidden | Sciencx - Accessed . https://www.scien.cx/2024/10/18/how-to-call-put-method-using-resttemplate-spring-boot/
IEEE
" » how to call put method using RestTemplate Spring Boot." realNameHidden | Sciencx [Online]. Available: https://www.scien.cx/2024/10/18/how-to-call-put-method-using-resttemplate-spring-boot/. [Accessed: ]
rf:citation
» how to call put method using RestTemplate Spring Boot | realNameHidden | Sciencx | https://www.scien.cx/2024/10/18/how-to-call-put-method-using-resttemplate-spring-boot/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.