‘@RequestBody’ annotation Maps Request Payload to given object
In the bleow example, @RequestBody Annotation Maps Request payload to Employee object.
@RequestMapping(value = "employees", method = RequestMethod.POST)
public ResponseEntity create(@RequestBody Employee emp) {
Employee persistedEmp = empRepo.add(emp);
return ResponseEntity.status(HttpStatus.CREATED).body(persistedEmp);
}
Find the below working application.
App.java
package com.sample.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here