Spring Boot RestTemplate postForEntity method

Producer app

EmployeeController

package com.example.demo.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springfra…


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

Producer app

Image description

EmployeeController

package com.example.demo.controller;

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

 @PostMapping("/save")
 public ResponseEntity<Employee> saveEmp(@RequestBody Employee emp){
  return new ResponseEntity<Employee>(emp,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

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

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.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/save";

  HttpHeaders header = new HttpHeaders();
  header.setContentType(MediaType.APPLICATION_JSON);

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

  HttpEntity<String> entity = new HttpEntity<String>(body,header);

  RestTemplate rt = new RestTemplate();

  //make http call
  ResponseEntity<Employee> response = rt.postForEntity(url, entity, Employee.class);

  System.out.println(response.getBody());

 }

}


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


Print Share Comment Cite Upload Translate Updates
APA

realNameHidden | Sciencx (2024-09-15T12:03:30+00:00) Spring Boot RestTemplate postForEntity method. Retrieved from https://www.scien.cx/2024/09/15/spring-boot-resttemplate-postforentity-method/

MLA
" » Spring Boot RestTemplate postForEntity method." realNameHidden | Sciencx - Sunday September 15, 2024, https://www.scien.cx/2024/09/15/spring-boot-resttemplate-postforentity-method/
HARVARD
realNameHidden | Sciencx Sunday September 15, 2024 » Spring Boot RestTemplate postForEntity method., viewed ,<https://www.scien.cx/2024/09/15/spring-boot-resttemplate-postforentity-method/>
VANCOUVER
realNameHidden | Sciencx - » Spring Boot RestTemplate postForEntity method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/15/spring-boot-resttemplate-postforentity-method/
CHICAGO
" » Spring Boot RestTemplate postForEntity method." realNameHidden | Sciencx - Accessed . https://www.scien.cx/2024/09/15/spring-boot-resttemplate-postforentity-method/
IEEE
" » Spring Boot RestTemplate postForEntity method." realNameHidden | Sciencx [Online]. Available: https://www.scien.cx/2024/09/15/spring-boot-resttemplate-postforentity-method/. [Accessed: ]
rf:citation
» Spring Boot RestTemplate postForEntity method | realNameHidden | Sciencx | https://www.scien.cx/2024/09/15/spring-boot-resttemplate-postforentity-method/ |

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.