In this post will see @RequestBody and @ResponseBody annotation example in Spring Boot.
Basic points about @RequestBody and @ResponseBody annotation.
@RequestBody annotation.
- This annotation introduced in Spring 3.0, available in org.springframework.web.bind.annotation package.
- It has one optional element i.e required.
- We use this annotation as the method parameter.
- It should be a DTO class or entity to map the request data attributes. In below example bookId,bookName and price have been defined in Book.java, same data we will pass from postman as request data.
- Since our controller class/endpoint will receive data in JSON format @RequestBody will convert into book object.
@ResponseBody annotation.
- This annotation introduced in Spring 3.0, available in org.springframework.web.bind.annotation package.
- We use this annotation with the method as well class.
- It converts object data to JSON response.
- If we use @RestController annotation, no need to use @ResponseBody.
@RequestBody and @ResponseBody annotation example in Spring Boot.
prerequisites –
- JDK 1.8
- Eclipse
- maven
- postman
Create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – requestbody, ArtifactId – requestbody and name – requestbody) and click on finish. Keep packaging as the jar.
Modify pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>requestbody</groupId> <artifactId>requestbody</artifactId> <version>0.0.1-SNAPSHOT</version> <name>requestbody</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
Book.java
package requestbodyexample; public class Book { int bookId; String bookName; String bookPrice; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookPrice() { return bookPrice; } public void setBookPrice(String bookPrice) { this.bookPrice = bookPrice; } }
BookController.java
package requestbodyexample; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @Controller @RequestMapping(value = "/test") public class BookController { @RequestMapping(value = "/simpleuri",method = RequestMethod.POST) @ResponseBody public Book getBook(@RequestBody Book book) { System.out.println(book.getBookName()); return book; } }
SpringMain.java
package requestbodyexample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class SpringMain { public static void main(final String[] args) { final ConfigurableApplicationContext configurableApplicationContext = SpringApplication .run(SpringMain.class, args); } }
If you encounter any port related issue, define application.properties and change the port.
application.properties
server.port = 9093
Run the main class and deploy the application.
Testing using postman.
POST – localhost:9093/test/simpleuri
In BookController.java we have –
public Book getBook(@RequestBody Book book) {
System.out.println(book.getBookName());
return book;
}
Suppose we want to save book entity in the database. We will send the book details from UI/browser and once control will come into the BookController, we have the all information in Book class. The attributes we are sending from UI should map with this Book class attributes. The @RequestBody annotation will convert those request JSON into java object. Now we have Book class object, we can add some business logic in the service layer and then using the repository method we can save the data into the database.
That’s all about @RequestBody and @ResponseBody annotation example in Spring Boot. You may like.
- @RestController and @Controller annotation example in Spring Boot.
- @RequestMapping annotation example In Spring Boot.
- @PathVariable and @RequestParam annotations in Spring Boot.
- @RequestHeader annotation example by using Spring Boot.
- @SpringBootApplication annotation example in Spring Boot.
- @Component, @Controller, @Service and @Repository annotations example using Spring Boot.
- @Configuration annotation example using spring boot.
- @ComponentScan example in spring boot.
Spring @RequestBody and @ResponseBody annotation docs.