Spring @Controller Annotation with Example

Hey guys in this post, we will discuss spring @Controller annotation with example.


@Controller annotation acts as a controller in the MVC design pattern. It indicates that the particular class serves the role of controller.

It contains methods that are annotated with @RequestMapping annotation with URL mappings that are triggered for web requests.

It works with view technology, so the method returns ModelAndView.

public class HomeController {
	public ModelAndView displayName () {
		ModelAndView mav = new ModelAndView("hello-world");
		//add data to model and view
		return mav;

Watch the video

Example on @Controller

The best way to understand @Controller is by looking at the example

Create spring boot project

There are many different ways to create a spring boot application, 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 controller annotation demo</description>




spring-boot-starter-web dependency for building web applications using Spring MVC.

It uses the tomcat as the default embedded container. spring-boot-devtools dependency for automatic reloads or live reload of applications. The spring-boot-starter-thymeleaf is a starter for building Spring MVC applications with Thymeleaf.

Create a controller

Create HomeController.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.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class HomeController {
	public ModelAndView displayName () {
		ModelAndView mav = new ModelAndView("hello-world");
		mav.addObject("name", "Bushan");
		return mav;

We have annotated the class with @Controller annotation, which indicates that the class serves the role of controller. The method returns the ModelAndView, which contains the view name and data that should display in the view template.

Create a view

Create hello-world.html inside the templates folder of src/main/resources and add the following content

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8">
<title>Insert title here</title>
	<p th:text="'My name is '+${name}"></p>

Inside the <p> element we are accessing the data using ${} that we have added to the ModelAndView.

Run the app

Run the application using the below maven command –

mvn spring-boot:run

Open the browser and navigate to the URL http://localhost:8080/name

About the author

Bushan Sirgur

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

View all posts

Leave a Reply

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