How to Make a Simple RestController in Spring Boot? Last Updated : 12 Mar, 2025 Comments Improve Suggest changes Like Article Like Report A RestController in Spring Boot is a specialized controller that is used to develop RESTful web services. It is marked with the @RestController annotation, which combines @Controller and @ResponseBody. This ensures that the response is automatically converted into JSON or XML, eliminating the need for explicit response conversions. In this article, we will explore how to create a simple RestController in Spring Boot.Steps to Create a Simple RestController in Spring BootStep 1: Setup Spring Boot Project Using Spring InitializrGo to Spring Initializr.Configure the project with the following details:Project: MavenLanguage: JavaSpring Boot Version: 3.x (Latest Version)Packaging: JARJava Version: 17 or laterDependencies: Spring WebClick on Generate to download the project.Step 2: Import the Project into Your IDEExtract the zip file. Now open a suitable IDE and then go to File > New > Project from existing sources and select pom.xml. Click on import changes on prompt and wait for dependencies to load.Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.Step 3: Create a Simple RestControllerGo to src > main > java > com.geeksforgeeks.SpringBootApplication, create a new Java class with the name Controller.java and add the annotation @RestController.Controller.java: Java package com.geeksforgeeks.SpringBootApplication.controller; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api") public class Controller { @GetMapping("/hello/{name}/{age}") public String sayHello(@PathVariable String name, @PathVariable int age) { return "Hello, " + name + "! You are " + age + " years old."; } } Explanation:@RestController annotation marks this class as a RESTful controller.@RequestMapping("/api") defines a base URL for all endpoints inside this controller.@GetMapping("/hello/{name}/{age}") handles HTTP GET requests and extracts name and age from the URL.@PathVariable annotation captures dynamic values from the request URL.Returns a simple JSON response with a greeting message.Step 4: Run the Spring Boot ApplicationRun the main class and wait for the Tomcat server to start.Note: The default port of the Tomcat server is 8080 and can be changed in the application.properties file.This Controller.java file is used for handling all incoming requests from the client-side.Once the application starts, test the API by opening a browser or using Postman:http://localhost:8080/api/hello/Sweta/25Response:"Hello, Sweta! You are 25 years old." Comment More infoAdvertise with us Next Article How to Make a Simple RestController in Spring Boot? Z zack_aayush Follow Improve Article Tags : Java Advance Java Java-Spring-Boot Practice Tags : Java Similar Reads Spring Boot - CrudRepository with Example Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se 6 min read Spring Boot - application.yml/application.yaml File Spring is widely used for creating scalable applications. For web applications Spring provides. In Spring Boot, whenever we create a new Spring Boot Application in spring starter, or inside an IDE (Eclipse or STS) a file is located inside the src/main/resources folder named as application.properties 4 min read Spring Boot - CRUD Operations using MySQL Database CRUD stands for Create, Read/Retrieve, Update and Delete and these are the four basic operations that we perform on persistence storage. CRUD is data-oriented and the standardized use of HTTP methods. HTTP has a few methods which work as CRUD operations and do note they are very vital from a develop 7 min read How to Implement One to Many Mapping in Spring Boot? Spring Boot is built on the top of the spring and contains all the features of spring. Spring also provides JPA and hibernate to increase the data manipulation efficiency between the spring application and the database. In very simple terms we can say JPA (Java persistence API) is like an interface 3 min read How to Run Your First Spring Boot Application in Eclipse IDE? Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se 3 min read How to Run Spring Boot Application? Spring Boot is built on the top of Spring and contains all the features of Spring. And it is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and set 8 min read Spring Boot - Application Properties Spring Boot is built on top of the Spring Framework and includes all its features while simplifying configuration and setup. It has become a favorite among developers because it provides a rapid, production-ready environment that allows them to focus on business logic instead of dealing with complex 3 min read Spring Boot with H2 Database H2 Database in Spring Boot is an embedded, open-source, and in-memory database. It is a relational database management system written in Java. It is a client/server application. It stores data in memory, not persist the data on disk. Here we will be discussing how can we configure and perform some b 6 min read Spring Boot - CRUD Operations CRUD stands for Create, Read/Retrieve, Update, and Delete and these are the four basic operations that we perform on persistence storage. CRUD is data-oriented and the standardized use of HTTP methods or there is a term for this called HTTP action verbs. HTTP has a few action verbs or methods that w 7 min read Spring Boot - REST Example In modern web development, most applications follow the Client-Server Architecture. The Client (frontend) interacts with the server (backend) to fetch or save data. This communication happens using the HTTP protocol. On the server, we expose a bunch of services that are accessible via the HTTP proto 4 min read Like