DEV Community

realNameHidden
realNameHidden

Posted on

1 1 1 1 1

Spring Boot RestTemplate postForEntity method

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

}

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

application.properties

spring.application.name=ProducerApp
server.port=8081

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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());

 }

}

Enter fullscreen mode Exit fullscreen mode

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay