Spring boot @ResponseBody annotation with example

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


@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).

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.

Watch the video

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">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot response body example</description>




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;

	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;

public class UserController {

	public @ResponseBody User getUser() {
		User user = new User();
		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


Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply