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.

Example

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

	
    @RestController
    public class BasicController {

        @GetMapping("/welcome")            
        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
  • 		
    			<dependency>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-starter-web</artifactId>
    			</dependency>
    													

    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();
    			this.id = id;
    			this.name = name;				
    			this.gender = gender;
    			this.age = age;				
    			this.salary = salary;
    		}
    
    		public long getId() {
    			return id;
    		}
    
    		public void setId(long id) {
    			this.id = id;
    		}
    
    		public String getName() {
    			return name;
    		}
    
    		public void setName(String name) {
    			this.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 );
    		employeeList.add(employee1);	
    		employeeList.add(employee2);
    		employeeList.add(employee3);
    		employeeList.add(employee4);
    	}
    												
  • 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