Spring boot @ResponseBody annotation with example





Hey guys in this post, we will discuss spring boot @ResponseBody annotation and their example.

Overview


@ResponseBody annotation can be put on a method and indicates that the method return type should bind to the HTTP response body (and not placed in a Model, or interpreted as a view name).

@GetMapping("/users")
public @ResponseBody User getUser() {

}

Under the hood, spring uses HTTP Message converters to convert the method return value to HTTP response body (serialize the object to response body), based on Content-Type present in the request HTTP header.

Example on @ResponseBody


Let’s understand @ResponseBody annotation with an example.

Create spring boot project


There are different ways to create a spring boot project, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies


Open pom.xml and add the following dependencies

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>springbootresponsebody</artifactId>
	<version>v1</version>
	<name>springbootresponsebody</name>
	<description>Spring boot response body example</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container. It also adds the jackson-databind which is needed for serialization in HttpMessageConverter.




spring-boot-devtools dependency for automatic reloads or live reload of applications.

Create model class


Create User.java inside the in.bushansirgur.springboot.model package and add the following content

package in.bushansirgur.springboot.model;

public class User {
	
	private String firstName;
	
	private String lastName;
	
	private Long age;

	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 Long getAge() {
		return age;
	}

	public void setAge(Long age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
	}
	
}

Create controller


Create UserController.java inside the in.bushansirgur.springboot.controller package and add the following content

package in.bushansirgur.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import in.bushansirgur.springboot.model.User;

@Controller
public class UserController {

	@GetMapping("/users")
	public @ResponseBody User getUser() {
		User user = new User();
		user.setFirstName("Bushan");
		user.setLastName("Sirgur");
		user.setAge(28L);
		return user;
	}
}

We will annotate @ResponseBody to the getUser() method. Now spring behind the scenes, uses HTTP message converter to convert a User object to HTTP response body with the help of jackson-databind

Run the application


You can run the application by executing the below command

mvn spring-boot:run

Open the browser and type the URL http://localhost:8080/users

Screenshot-2021-02-16-at-11-47-44-PM



About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *