How to Create a Rest Controller in Spring Boot
In this topic, we are going to learn RestController in Spring Boot.
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 RestController class in Spring Boot Project?
4.1 Keep eclipse IDE ready
4.2 Create a Spring Boot Starter Project(Select Spring Web dependency)
4.3 Create RestController class
4.4 Run the Project
5. Conclusion
1. What is RestController
RestController is useful when we are working on a rest API Spring Application. This RestController gives us JSON(JavaScript Object Notation) as response output. A normal class is annotated with “@RestController” then that class is treated as RestController.
Developing the Rest Api application in Spring Boot is taking little time to ready production-level applications.
2. Difference between @Controller and @RestController:
@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: Informs 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 RestController class in Spring Boot Project?
4.1. Keep 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. Create a Spring Boot Starter Project
Add the following dependencies:
• Spring Web
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 Create RestController 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 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.
5. Conclusion:
In this topic, we learned about RestController, the Difference between Controller and RestController classes and configure the RestController project in Spring Boot.
Leave your thought here
Your email address will not be published. Required fields are marked *