Hello Java programmers, if you are wondering how and when to use use the @RequestBody and @ResponseBody annotation in Java then you are at the right place. Earlier, I have shared how to create RESTful Web Service using Spring Boot and today I will teach you how and when to use these two crucial annotations @RequestBody and @ResponseBody. While developing client server Java application using Spring Framework you may need to connect the HTTP request and response body with the domain object while working on the REST API. The annotations @RequestBody and @ResponseBody in Spring were used to bind these HTTP requests and responses. In simple words, these are the annotations which converts JSON to your Java object and your data to JSON while sending to client. Let's look at these two Spring annotations in more detail.
How to use @RequestBody and @ResponseBody Annotations in Spring
As the name suggest @RequestBody annotation is used to parse the incoming HTTP request while @ResponseBody annotation is used to convert your object into HTTP response in the form client is expecting like JSON, XML, or simply text. This is the basic, now let's look at these two annotations in detail.
1. @RequestBody annotation
Below is our data model.
package com.restdao.mydata; public class Registration { private String firstName; private String lastName; private String email; private int age; private String password; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Controller:
@RestController public class RegistrationController { @Autowired private CricketerService cricketerService; @PostMapping("/registration") public ResponseEntity < Cricketer > register(@RequestBody Registration registration) { Cricketer cricketer = cricketerService.saveCricketer(mapCricketerData(registration)); return new ResponseEntity < Cricketer > (cricketer, HttpStatus.OK); } protected Cricketer mapCricketerData(Registration registration) { Cricketer cricketer = new Cricketer(registration.getFirstName(),
registration.getLastName(), registration.getEmail()); cricketer.setAge(registration.getAge()); return cricketer; } }
{ "firstName":"first", "lastName":"last", "email":"sample@www.iplt20.com", "age":"24" }
application/json;charset=UTF-8 transfer-encoding:chunked date:Tue, 10 April 2022 02:49:39 GMT { "id": 5, "firstName": "first", "lastName": "last", "email": "sample@www.iplt20.com", "age": 24 }
2. @ResponseBody annotation in Spring MVC
REST Controller:
@RestController public class RegistrationController { @Autowired private CricketerService cricketerService; @PostMapping("/registration") public ResponseEntity < Cricketer > register(@RequestBody Registration registration) { Cricketer cricketer = cricketerService.saveCricketer(mapCricketerData(registration)); return new ResponseEntity < Cricketer > (cricketer, HttpStatus.OK); } protected Cricketer mapCricketerData(Registration registration) { Cricketer cricketer = new Cricketer(registration.getFirstName(),
registration.getLastName(), registration.getEmail()); cricketer.setAge(registration.getAge()); return cricketer; } }
@Controller public class RegController { @Autowired private CricketerService cricketerService; @PostMapping("/new-registration") public @ResponseBody Cricketer register(@RequestBody Registration registration) { return cricketerService.saveCricketer(mapCustomerData(registration)); } }
- How to enable Spring security in a Java web application?
- Spring Boot + Angular Example for Fullstack Developers
- 5 Best Courses to learn Reactive Spring and WebFlux
- How to use @SpringBootTest annotation in Java
- How to use MyBatis with Spring Boot in Java?
- My Favorite Spring Boot Courses for Java programmers
- Spring Boot + React.js example for Java developers
- 15 Spring Data JPA Interview Questions with Answers
- How to log SQL in a Spring Boot application?
- How to fix No Spring Bean found of Type using @Autowired
- 6 Best Courses to learn Spring Framework
- 25+ Spring Security Interview Questions with Answers
- 20+ Spring Boot Interview Questions with Answers
- What is the use of DispatcherServlet in Spring MVC?
- How to create RESTful Web Service in Java using Spring
Thanks a lot for reading this article so far. If you like this Java and Spring Boot tutorial then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note.
No comments :
Post a Comment