Spring Controller – Spring MVC Controller

Filed Under: Spring

Spring Controller annotation is a specialization of @Component annotation. Spring Controller annotation is typically used in combination with annotated handler methods based on the RequestMapping annotation.

Spring Controller

Spring Controller annotation can be applied on classes only. It’s used to mark a class as a web request handler. It’s mostly used with Spring MVC application.

Spring RestController

Spring @RestController is a convenience annotation that is itself annotated with @Controller and @ResponseBody. This annotation is used to mark a class as request handler for RESTful web services.

Spring Controller Example

Let’s create a simple spring application where we will implement standard MVC controller as well as REST controller.

Create a “Dynamic Web Project” in Eclipse and then convert it to Maven project. This will provide us with maven based web application structure and we can build our application on top of it.

Below image shows the final project structure of our Spring MVC Controller application.

Spring Controller Example Project

We would need following dependencies for our application.


<!-- Jackson for REST -->

Let’s look at the deployment descriptor (web.xml) where we will configure DispatcherServlet servlet as the front controller.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://xmlns.jcp.org/xml/ns/javaee" 
 xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <!-- Add Spring MVC DispatcherServlet as front controller -->

Finally, we have following spring context file. Here we are configuring our application to be annotation-based and providing root package for scanning spring components. We are also configuring InternalResourceViewResolver bean and providing details of view pages.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:beans="https://www.springframework.org/schema/beans"
	xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<context:component-scan base-package="com.journaldev.spring" />

	<!-- Resolves views selected for rendering by @Controllers to JSP resources 
		in the /WEB-INF/views directory -->
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />


Our configuration XML files are ready, let’s move on to the Controller class now.

package com.journaldev.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

public class HomeController {

	public String home(Locale locale, Model model) {
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);
		model.addAttribute("serverTime", formattedDate);
		return "home";


We have defined a single request handler method, it’s accepting GET requests with URI “/hello” and returning “home.jsp” page as the response. Notice that we are setting an attribute to the model, which will be used in the home.jsp page.

Here is our simple home.jsp page code.

<%@ page language="java" contentType="text/html; charset=UTF-8"
	<h1>Hello world!</h1>

	<p>The time on the server is ${serverTime}.</p>


Spring MVC Controller Test

Our conventional servlet based Spring MVC application with a simple controller is ready, just export it as the WAR file and deploy on Tomcat or any other servlet container.

Then go to URL https://localhost:8080/Spring-Controller/hello and you should see the following screen as output.

Spring Controller Example

Spring RestController Example

Now let’s extend our application to expose REST APIs too. Create a model class that will be sent as JSON response.

package com.journaldev.spring.model;

public class Person {

	private String name;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

Here is our simple REST Controller class.

package com.journaldev.spring.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.journaldev.spring.model.Person;

public class PersonRESTController {

	public String healthCheck() {
		return "OK";

	public Person getPerson(@RequestParam(name = "name", required = false, defaultValue = "Unknown") String name) {
		Person person = new Person();
		return person;


Redeploy the application again to test our REST APIs.

Spring REST Controller Test

Go to URL https://localhost:8080/Spring-Controller/rest and you should get following output.

Spring MVC Controller

Go to URL https://localhost:8080/Spring-Controller/rest/person/get and you will get following JSON response:

Spring REST Controller

Now let’s provide the name parameter value in the URL, go to https://localhost:8080/Spring-Controller/rest/person/get?name=Pankaj and you will get following JSON response.

Spring REST Controller Example


Spring Controller is the backbone of Spring MVC applications. This is where our business logic starts. Furthermore, RestController helps us in creating rest based web services easily.

You can download the example project code from our GitHub Repository.


  1. Patrick says:

    I would like to note that putting “get” in your route endpoint isn’t very RESTful. In REST, the verb is expected to be the HTTP action. In that sense, your endpoint would just be “rest/people”.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors