Spring RequestBody and ResponseBody Annotations

Updated on December 3rd, 2019

As part of the REST API with Spring series, in this post, we will get an overview of the Spring @RequestBody and @ResponseBody annotations.

 

Introduction

While working on the REST API, we may need to bind the HTTP request and response body with the domain object. Spring @RequestBody and @ResponseBody annotations used to bind these HTTP request/response. Let’s have a closer look at these two Spring annotations.

 

1. @RequestBody

To put it in layman terms, the @RequestBody annotation binds the HTTPRequest body to the domain object. Spring framework automatically deserializes incoming HTTPRequest to the Java object using Http Message Converters .We pass the body of the request through a HttpMessageConverter to resolve the method argument depending on the content type of the request. To understand this, let’s take an example of the customer registration process.

  • API need customer data for the registration.
  • Customer full registration form, system will send customer data through HTTPRequest body.

Here is our registration data model.

package com.javadevjournal.restexample.data;

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;
 }
}

Let’s create our REST control to accept this registration data.

@RestController
public class RegistrationController {

 @Autowired
 private CustomerService customerService;

 @PostMapping("/registration")
 public ResponseEntity < Customer > register(@RequestBody Registration registration) {
  Customer customer = customerService.saveCustomer(mapCustomerData(registration));
  return new ResponseEntity < Customer > (customer, HttpStatus.OK);
 }

 protected Customer mapCustomerData(Registration registration) {
  Customer customer = new Customer(registration.getFirstName(), registration.getLastName(), registration.getEmail());
  customer.setAge(registration.getAge());
  return customer;
 }
}

There are few important things in the above code. 

  • Spring framework will automatically deserialize the JSON into a Java type based on the HTTPMessageConvertor.

To test our application, use any REST Client (Postman etc.), send a POST request to the following URL http(s)://host: port/registration with similar data.

{
  "firstName":"first",
  "lastName":"last",
  "email":"[email protected]",
   "age":"30"
}

We will get the following response from our REST Controller.

application/json;charset=UTF-8
transfer-encoding:chunked
date:Tue, 07 Aug 2018 02:49:39 GMT
{
"id": 5,
"firstName": "first",
"lastName": "last",
"email": "[email protected]",
"age": 30
}

As seen in this example, Spring automatically converting incoming JSON data in the HTTPRequestBody to the Registration java object.

 

2. @ResponseBody

To put this in simple words, @ResponseBody tell Spring framework to serialize a return object into JSON or XML and send this information back as part of the HTTPResponse.

With Spring 4.x, If we are using working on the REST API, we should not use @ResponseBody on method level, but <@RestController on a class level.@RestController is a composed annotation that is itself a meta-annotated with @Controller and @ResponseBody.Let’s look at the previous example, our register method is returning Customer object and we like this to return as  JSON in the HTTPResponse.Here is our Customer model.

public class Customer {


 private Long id;
 private String firstName;
 private String lastName;
 private String email;
 private int age;

 public Customer() {}

 public Customer(String firstName, String lastName, String email) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.email = email;
 }

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 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;
 }
}

When we sent a POST request to our REST controller, we may get the following response from the controller

application/json;charset=UTF-8
transfer-encoding:chunked
date:Tue, 07 Aug 2018 02:49:39 GMT
{
"id": 5,
"firstName": "first",
"lastName": "last",
"email": "[email protected]",
"age": 30
}

Please note that we have not annotated our method with @ResponseBody annotation since @RestController annotation itself meta-annotated with @Controller and the @ResponseBody.

In case we do not have the option to use @RestController annotation, we need the following changes in our controller.

@Controller
public class RegController {

 @Autowired
 private CustomerService customerService;

 @PostMapping("/new-registration")
 public @ResponseBody Customer register(@RequestBody Registration registration) {
  return customerService.saveCustomer(mapCustomerData(registration));
 }
}

 

Summary

In this article, we covered Spring @RequestBody and @ResponseBody annotations. These annotations are important while building our REST API using Spring and Spring Boot. The code for this post is available on the GitHub.

Java Development Journal

Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.

follow me on:

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of