The @ResponseBody annotation in Spring is used to directly send the return value of a controller method as an HTTP response, instead of resolving it to a view. It helps in building RESTful web services where data is returned in JSON or XML format.
- Automatically converts Java objects into JSON/XML using message converters.
- Eliminates the need for view pages like JSP or Thymeleaf in REST APIs.
- Commonly used in REST controllers to send data to frontend or client applications.

Real-World Uses
- Building RESTful web services
- Sending data to frontend (Angular/React/JS apps)
- Mobile app backend APIs
- Microservices communication
Prerequisites
- Eclipse (EE version)/STS IDE
- Spring JAR Files
- Tomcat Apache latest version
Steps to implements @ResponseBody Annotation in Spring MVC
Follow these steps to create @ResponseBody Annotation in Spring MVC.
Step 1: Create Dynamic Web Project
- Open Eclipse IDE
- Create a Dynamic Web Project
- Give project name (e.g., myfirst-mvc-project)
Step 2: Add Spring JAR Files
- Download Spring JARs
- Go to - src - main - webapp - WEB-INF - lib
- Paste all Spring JAR files here
Step 3: Configure Tomcat Server
Refer to this article Configuration of Apache Tomcat Server and configure the tomcat server with your application. Now we are ready to go.
Step 4: Configure DispatcherServlet (web.xml)
Configuring Dispatcher Servlet: Go to the src > main > webapp > WEB-INF > web.xml file and the complete code for web.xml file is given below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html"
xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>myfirst-mvc-project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<absolute-ordering/>
<servlet>
<!-- Provide a Servlet Name -->
<servlet-name>frontcontroller-dispatcher</servlet-name>
<!-- Provide a fully qualified path to the DispatcherServlet class -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!-- Provide a Servlet Name that you want to map -->
<servlet-name>frontcontroller-dispatcher</servlet-name>
<!-- Provide a url pattern -->
<url-pattern>/student.com/*</url-pattern>
</servlet-mapping>
</web-app>
Step 5: Create Spring Configuration File
Now go to the src > main > webapp > WEB-INF and create an XML file. Actually, this is a Spring Configuration file like beans.xml file and the name of the file must be in this format -> frontcontroller-dispatcher-servlet.xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context/"
xsi:schemaLocation="http://www.springframework.org/schema/beans/
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context/
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.student.controllers"></context:component-scan>
</beans>
Step 6: Configure Component Scanning
Add component scan in XML file:
<context:component-scan base-package="com.student.controllers"></context:component-scan>
Step 7: Create Controller Class
In this step, the class is marked as a Spring MVC controller using @Controller to handle incoming HTTP requests. The method mapped with @RequestMapping returns data directly in the HTTP response using @ResponseBody instead of rendering a view.
package com.student.controllers;
// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
// Annotation
@Controller
// Class
public class DemoController {
// Annotation
@ResponseBody
@RequestMapping("/hello")
// Method
public String helloWorld()
{
return "Hello World!";
}
}
Modern Approach (@RestController): In this modern approach, @RestController is used which combines @Controller and @ResponseBody into a single annotation. It directly returns the method response as HTTP output, making it ideal for building RESTful web services.
Step 8: Run Application on Server
To run your Spring MVC Application right-click on your project > Run As > Run on Server and run your application as shown in the below image.

After that use the following URL to run your controller as shown in the below image. All other details are mentioned in the image.
http://localhost:8080/myfirst-mvc-project/student.com/hello
Output:

Explanation: When a client hits the /hello URL, the request is processed by DispatcherServlet, which maps it to the corresponding controller method. The method returns a string value, and because of @ResponseBody, Spring directly writes this return value into the HTTP response body instead of resolving a view. The client receives the output as plain text in the browser or API response.