Open In App

Spring @Controller Annotation with Example

Improve
Improve
Like Article
Like
Save
Share
Report

Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier than compared to classic Java frameworks and application programming interfaces (APIs), such as Java database connectivity (JDBC), JavaServer Pages(JSP), and Java Servlet. This framework uses various new techniques such as Aspect-Oriented Programming (AOP), Plain Old Java Object (POJO), and dependency injection (DI), to develop enterprise applications. Now talking about Spring Annotation

Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. 

There are many annotations are available in Spring Framework. Some of the Spring Framework Annotations are listed below as follows where here we are going to discuss one of the most important annotations that is @Controller Annotation

  • @Required
  • @Autowired
  • @Configuration
  • @ComponentScan
  • @Bean
  • @Component
  • @Controller
  • @Service
  • @Repository, etc.

@Controller Annotation

Spring @Controller annotation is also a specialization of @Component annotation. The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It’s used to mark a class as a web request handler. It’s mostly used with Spring MVC applications. This annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations. Let’s understand all of these by example. 

Procedure

  1. Create a simple Spring Boot project
  2. Add the spring-web dependency in your pom.xml file
  3. Create one package and name it as “controller”
  4. Create a class inside the package
  5. Run our application inside the DemoApplication.java file

Step 1: Create a Simple Spring Boot Project

Refer to this article Create and Setup Spring Boot Project in Eclipse IDE and create a simple spring boot project. 

Step 2: Add the spring-web dependency in your pom.xml file. Go to the pom.xml file inside your project and add the following spring-web dependency.

XML




<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>


Step 3: In your project create one package and name the package as “controller”. In the controller package create a class and name it as DemoController. This is going to be our final project structure.

Example

Java




// Java Program to Illustrate DemoController File
  
// Importing package in this code module
package com.example.demo.controller;
// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
  
// Annotation
@Controller
// Main class
public class DemoController {
  
    @RequestMapping("/hello")
    @ResponseBody
  
    // Method
    public String helloGFG()
    {
        return "Hello GeeksForGeeks";
    }
}


We have used the below annotations in our controller layer. Here in this example, the URI path is /hello.

  • @Controller: This is used to specify the controller.
  • @RequestMapping: This is used to map to the Spring MVC controller method.
  • @ResponseBody: Used to bind the HTTP response body with a domain object in the return type.

Step 4: Now, our controller is ready. Let’s run our application inside the DemoApplication.java file. There is no need to change anything inside the DemoApplication.java file. 

Example

Java




// Java Program to Illustrate DemoApplication File
  
// Importing package in this code module
package com.example.demo;
// Importing required classes
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
  
// Annotation
@SpringBootApplication
  
// Main class
public class DemoApplication {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        SpringApplication.run(DemoApplication.class, args);
    }
}


Output:

Tip: Try Tomcat URL depicted in below media, which is running on http://localhost:8989/hello



Last Updated : 03 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads