How to Create a Rest Controller in Spring Boot

In this topic, we are going to learn how to create a Rest Controller in Spring Boot, What is a @RestController annotation and its use in Spring Boot Application, Difference between @Controller and @RestController. We will create a Spring Project using Spring Web and Maven.

Table of content
1. What is a @RestController?
2. Difference between @Controller and @RestController
3. What are the annotations enabled by using @SpringBootApplication?
4. How to Create a Rest Controller class in Spring Boot Project?
    4.1 Keep the Eclipse IDE ready
    4.2 Creating a Spring Boot Starter Project
    4.3 Creating a Rest Controller class
    4.4 Run the Project
5. Conclusion

1. What is a Rest Controller?

A Rest Controller is useful when we are working on a rest API Spring Application. This Rest Controller gives us JSON(JavaScript Object Notation) as response output. A normal class is annotated with @RestController then that class is treated as Rest Controller. Developing the Rest API application in Spring Boot takes little time to ready production-level applications as compared to Spring Web with XML configuration.

2. Difference between @Controller and @RestController


We demonstrate the difference between these two annotations below here:

@Controller
@ResponseBody
class TestController{
----------------
}
@RestController
class TestController{
 ----------------
}	

→ Spring 4.0 introduced the @RestController for the creation of the Restful web services in a simple manner. It is a specialized kind of @Controller annotation.
→ In @Restcontroller we don’t need to use @ResponseBody in the handler method. In this @RestController we cannot return the view as output.
→ A normal class is annotated with @Controller then that class is treated as a Controller class in Spring MVC.
→ Spring 2.5 introduced the @Controller for web application return as a view in Spring MVC. It is a specialized kind of @Component annotation.
→ In @Controller we need to use @ReponseBody in every handler method for response output without the view.
→ @RestController: This combination of @Controller and @ResponseBody.This will be used when we are making Rest Apis.

3. What are the annotations enabled by using @SpringBootApplication?


• @SpringBootApplication is enabling these annotations @Configuration,@EnableAutoConfiguration and @ComponentScan
• @Configuration: It ables to register extra beans with @Bean or import other configuration classes
• @EnableAutoConfiguration: Inform Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if Spring Web MVC is on the classpath, this annotation flags the application as a web application and activates key behaviours, such as setting up a DispatcherServlet.
• @ComponentScan: It allows component scanning of the current package and also recursively scans sub-packages.

4. How to Create a Rest Controller class in Spring Boot Project?

 4.1. Keep the Eclipse IDE ready(STS Integrated) 

Refer to this article How to Create Spring Project in IDE to create Spring Boot Project in Eclipse IDE.

4.2. Creating a Spring Boot Starter Project 

Add the following dependencies: 
            • Spring Web       

Project Structure

how_to_create_a_rest_controller_in_spring_boot

Maven Dependency

<?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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.poc</groupId>
	<artifactId>Spring_Boot_Configure_RestController_Proj</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring_Boot_Configure_RestController_Proj</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
package com.poc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootConfigureProjApplication {

	public static void main(String[] args) {
		
		SpringApplication.run(SpringBootConfigureProjApplication.class, args);
	}

}

4.3 Creating a Rest Controller class

HomeController.java

package com.poc.controller;

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

@RestController
public class HomeController {
	@GetMapping("/")
	public String message() {
		return"---------Hello Spring Boot App------------";
	}
}

→ We can use @RestController and @GetMapping annotations in the Controller class. 
→ @GetMapping: It will handle requests from the HTTP Get Method (Client).

4.4 Run the Project


Right-click on this class SpringBootConfigureRestControllerProjApplication (automatically generated the main class) of this project. Click Run as Java Application.
Go to Web Browser and type this URL http://localhost:8080 for testing.

how_to_create_rest_controller_in_spring_boot


5. Conclusion


In this topic, we learned about the Rest Controller, the Difference between Controller and Rest Controller classes and configured the Rest Controller project in Spring Boot.

Leave a Comment