Spring @RestController Annotation with Example

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


@RestController annotation is mainly used for building restful web services using Spring MVC. It is a convenience annotation, this annotation itself annotated with @ResponseBody and @Controller annotation. The class annotated with @RestController annotation returns JSON response in all the methods.

It does not work with view technology so that the method does not return ModelAndView.

public class CustomerController {
	public Customer getCustomer () {

Watch the video

Example on @RestController

The best way to understand @RestController 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 restcontroller 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.

Create an entity class

Create Customer.java inside the in.bushansirgur.springboot.entity package and add the following content

package in.bushansirgur.springboot.entity;

public class Customer {
	private String name;
	private String location; 
	public Customer(String name, String location) {
		this.name = name;
		this.location = location;

	public String getName() {
		return name;

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

	public String getLocation() {
		return location;

	public void setLocation(String location) {
		this.location = location;

	public String toString() {
		return "Customer [name=" + name + ", location=" + location + "]";

This is just a plain old java class that has private fields, setters, getters, and constructors.

Create a Rest controller

Create CustomerController.java inside the in.bushansirgur.springboot.controller package and add the following content

package in.bushansirgur.springboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import in.bushansirgur.springboot.entity.Customer;

public class CustomerController {
	public Customer getCustomer () {
		Customer c = new Customer("Bushan", "India");
		return c;

We have annotated the class with @RestController annotation, this indicates that the class serves the role of controller and also returns JSON response from all the methods.

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/customer

    "name": "Bushan",
    "location": "India"

Bushan Sirgur

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

Leave a Reply