Understanding Controllers in Spring Boot with Examples

Saurabh Mhatre
3 min readNov 1, 2023

--

Title Image

Controllers play a crucial role in Spring Boot applications, as they handle incoming HTTP requests and determine the appropriate response to send back. In this article, we’ll explore controllers in Spring Boot, including how to create them, handle different types of requests, and return responses. We’ll also provide examples to illustrate these concepts.

Introduction to Controllers

What are Controllers?
Controllers in Spring Boot are Java classes responsible for handling incoming HTTP requests and returning an appropriate response. They act as intermediaries between the client (usually a web browser or a mobile app) and the application’s business logic.

The Role of Controllers in Spring Boot
Controllers serve as the entry point for requests to your application. They receive requests, process the necessary data, and determine the appropriate response. This can include returning HTML pages, JSON data for APIs, or even redirecting to other URLs.

Creating a Controller

Setting Up a Spring Boot Project
Before creating a controller, you’ll need to set up a Spring Boot project. You can do this by using Spring Initializr (https://start.spring.io/) or your preferred IDE.

Creating a Controller Class
In Spring Boot, a controller is a Java class annotated with `@Controller` or `@RestController` (for APIs). Here’s an example of a simple controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

@GetMapping("/hello")
@ResponseBody
public String sayHello() {
return "Hello, World!";
}
}

In this example, we’ve created a controller named `HelloController`. It has a method `sayHello()` that handles GET requests to the `/hello` endpoint. The `@GetMapping` annotation maps the method to the specified URL, and `@ResponseBody` indicates that the return value should be used directly as the HTTP response.

Handling Different Types of Requests

Handling GET Requests

GET requests are used to retrieve data from the server. Here’s an example of handling a GET request:

@GetMapping("/user/{id}")
@ResponseBody
public String getUser(@PathVariable int id) {
// Logic to retrieve user with the given ID
return "User ID: " + id;
}

In this example, the `@PathVariable` annotation is used to extract the `id` from the URL path.

Handling POST Requests

POST requests are used to send data to the server. Here’s an example of handling a POST request:

@PostMapping("/user")
@ResponseBody
public String createUser(@RequestBody User user) {
// Logic to create a new user
return "User created: " + user.getName();
}

Handling PUT and DELETE Requests

PUT requests are used to update existing data, while DELETE requests are used to remove data. Here’s an example of handling PUT and DELETE requests:

@PutMapping("/user/{id}")
@ResponseBody
public String updateUser(@PathVariable int id, @RequestBody User user) {
// Logic to update user with the given ID
return "User updated: " + user.getName();
}

@DeleteMapping("/user/{id}")
@ResponseBody
public String deleteUser(@PathVariable int id) {
// Logic to delete user with the given ID
return "User deleted: " + id;
}

These examples demonstrate how to handle different types of requests in a Spring Boot controller. Have a nice day ahead!

Check out my Youtube channel for more content:
SaurabhNative-Youtube

--

--

Saurabh Mhatre
Saurabh Mhatre

Written by Saurabh Mhatre

Senior Frontend Developer with 9+ years industry experience. Content creator on Youtube and Medium. LinkedIn/Twitter/Instagram: @SaurabhNative

No responses yet