Spring Boot is a widely used framework for building robust and production-ready Java applications with minimal configuration. It simplifies web application development by combining powerful features such as Dependency Injection (DI), Aspect-Oriented Programming (AOP), and built-in support for RESTful web services.
In this article, we’ll explore the @Controller annotation in Spring Boot and understand its purpose, behavior, and usage through a practical example.
@Controller Annotation in Spring Boot
The @Controller annotation is a specialized form of the @Component annotation in the Spring Framework. It is used to mark a class as a Spring MVC Controller, responsible for handling web requests and returning responses.
Key Points:
- It is a part of the Spring MVC architecture.
- It is typically used with @RequestMapping to map HTTP requests to specific methods.
- It can only be applied at the class level.
- During component scanning, Spring automatically detects and registers classes annotated with @Controller.
Steps to Use the @Controller Annotation
Step 1: Create a Spring Boot Project
Create a new Spring Boot project using your preferred IDE (Eclipse, IntelliJ IDEA, or Spring Initializr).
If using Spring Initializr, select:
- Project: Maven
- Dependencies: Spring Web
Step 2: Add the spring-web dependency
In case your project does not include it by default, add the following dependency to your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
This provides the necessary libraries for creating web applications and REST APIs.
Step 3: Create a Controller Package
Create a package named controller under src/main/java/com/example/demo/. This package will contain the controller class that handles web requests.

Step 4: Create a Controller Class
Inside the controller package, create a file named DemoController.java.
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DemoController {
@RequestMapping("/hello")
@ResponseBody
public String helloGFG() {
return "Hello GeeksForGeeks";
}
}
- @Controller: Marks the class as a Spring MVC controller.
- @RequestMapping("/hello"): Maps the
/helloURL to thehelloGFG()method. - @ResponseBody: Instructs Spring to return the method result directly as the HTTP response body, rather than looking for a view.
Step 5: Create the Main Application Class
Create a main class DemoApplication.java under the base package com.example.demo.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@SpringBootApplication combines three annotations:
- @Configuration: Marks the class as a source of bean definitions.
- @EnableAutoConfiguration: Enables automatic configuration based on classpath dependencies.
- @ComponentScan: Scans the package for annotated components like @Controller,
@Service, and@Repository.
Step 6: Run the Application
Run the DemoApplication class as a Java Application. By default, the application starts on port 8080.
Access the endpoint at
http://localhost:8080/hello
If you want to change the port, modify the application.properties file:
server.port=8989

Output:
