How the request body of form is passed to the spring MVC controller and how to map to the POJO class instance?
CodePudding user response:
I assume you are using POST
API request for your use case. In the spring mvc controller class, we can make use of @RequestBody
annotation followed by Pojo class.
Example:
@PostMapping
public ResponseEntity<ResponseDto> saveData(@RequestBody Data data) {
// Access to service layer
}
CodePudding user response:
Let's say you're doing a POST request.
@PostMapping
public void saveSomeData(@RequestBody PojoClass pojoClass){
// whatever you wanna do
}
The @RequestBody annotation extracts the data your client application is sending on the body of the request. Also, you'd want to name the member variables on your pojo class similar to how you named it in on your request body.
Your POJO class can be something like:
public class PojoClass{
private String name;
private int age;
}
Now you can create getters and setters or use the Lombok annotation @Data
on the class level to auto-generate the getters and setters.
Hope this was helpful:)