Use of @RestController Annotation

@RestController is used to implement REST endpoints.

It is stereotype annotation, like @Component.

By Stereotype it means, that annotations denote specific roles at conceptual level.


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

    public class BasicController {

        public String welcome() {
			return " Hello World ";

In the example we can see two annotation, @RestController and @GetMapping.

@RestController tells Spring that this component is to serve as a REST controller.

The @RestController annotation provides a combination of @ResponseBody and @Controller annotations.

Lets create a Simple REST Service and understand the usage of @RestController.

Procedure :

  • Create project using STS (Spring Starter Project) or Spring Initializr.
  • Project structure will look like this :

    Fig : Project Structure
  • Add a Web Dependency in your pom.xml

    or type web in Search for dependencies search box, add web dependency and download zipped project.

  • Create a Model Class (i.e. Employee) in model package.
    package com.tutorialbox.model;
    	public class Employee {											
    		private long id;
    		private String name;
    		private String gender;
    		private int age;
    		private double salary;
    		public Employee( long id, String name, int age, String gender, double salary ) 
    			super(); = id; = name;				
    			this.gender = gender;
    			this.age = age;				
    			this.salary = salary;
    		public long getId() {
    			return id;
    		public void setId(long id) { = id;
    		public String getName() {
    			return name;
    		public void setName(String name) { = name;
    		public String getGender() {
    			return gender;
    		public void setGender(String gender) {
    			this.gender = gender;
    		public int getAge() {
    			return age;
    		public void setAge(int age) {
    			this.age = age;
    		public double getSalary() {
    			return salary;
    		public void setSalary(double salary) {
    			this.salary = salary;
  • Create EmployeeController class and add @RestController annotations.
  • 		@RestController
    		public class EmployeeController {
  • Define static List employee variable to simulate database and add 4 employees to the list.
  • 	private static List<Employee> employeeList = new ArrayList<Employee>();
    	public EmployeeController() {
    		Employee employee1 = new Employee(101,"Rahul",23,60000 );
    		Employee employee2 = new Employee(102,"Shreya",23,70000 );
    		Employee employee3 = new Employee(103,"Arun",23,65000 );
    		Employee employee4 = new Employee(104,"Radhika",23,55000 );
  • Create new method to return all employees in static list (getEmployees)
  • 		@GetMapping("/employees")
    		public List<Employee> getEmployees() { 
    			return employeeList;
  • Run the application and call this URL http://localhost:8080/employees
  • You will see the result as :

    Fig : Output