Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring REST: Map POST payload to an entity

Spring REST: Map POST Payload To An Entity

 ‘@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

Share the post

Spring REST: Map POST payload to an entity

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×