Create Project: bootspring_http_requestbody (add Spring Boot Starters from the table)
Create Package: DTO (inside main package)
– Create Class: PersonDTO.java (inside package DTO)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
PersonDTO.java
package com.ivoronline.bootspring_http_requestbody.DTO;
public class PersonDTO {
//PROPERTIES
//Used for Deserialization if there is no Constructor or Setters
//Jackson uses reflection to access private Properties.
public Long id;
public String name;
public Integer age;
}
MyController.java
package com.ivoronline.bootspring_http_requestbody.controllers;
import com.ivoronline.bootspring_http_requestbody.DTO.PersonDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/AddPerson")
public String addPerson(@RequestBody PersonDTO personDTO) {
//GET DATA FROM PersonDTO
String name = personDTO.name;
Integer age = personDTO.age;
//RETURN SOMETHING
return name + " is " + age + " years old";
}
}