Featured Article

Spring Boot Tutorial Controller Mastery Guide for Beginners

Kenneth Jul 13, 2026

Spring Boot is a popular Java-based framework that simplifies the creation of stand-alone, production-grade applications. One of the key components in a Spring Boot application is the Controller, which handles the business logic for HTTP requests and responses. In this tutorial, we'll explore how to create and manage Spring Boot controllers.

Spring Boot Project Structure for Clean & Scalable Code | Raghvendra Pratap posted on the topic | LinkedIn
Spring Boot Project Structure for Clean & Scalable Code | Raghvendra Pratap posted on the topic | LinkedIn

Spring Boot's approach to controllers is deeply rooted in the Model-View-Controller (MVC) architectural pattern. It provides a clear separation between the business logic (Controller) and the presentation layer (View), which makes applications more modular, testable, and maintainable. Let's dive in.

How To Develop REST API Using Spring Boot ?
How To Develop REST API Using Spring Boot ?

Creating a Basic Spring Boot Controller

A Spring Boot controller is a class annotated with the @Controller or @RestController annotation. While both annotations serve the same purpose, @RestController is more commonly used as it already includes the @ResponseBody annotation, offering a lightweight RESTful approach.

Spring Boot Error Handling Unleashed: Customizing the /error Path with Your Controller
Spring Boot Error Handling Unleashed: Customizing the /error Path with Your Controller

Here's a basic example of a Spring Boot controller:

Hello World Controller

Spring Boot cheatsheet - Masterspringboot
Spring Boot cheatsheet - Masterspringboot

First, let's create a simple controller that responds with a "Hello, World!" message.

```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @GetMapping("/hello-world") public String sayHello() { return "Hello, World!"; } } ``` In this example, the @GetMapping annotation maps the HTTP GET request for the "/hello-world" URL to the `sayHello` method. The method returns a simple string, which is automatically converted to a JSON response by Spring Boot.

Request Parameters and Path Variables

Testing Spring Boot REST Controller Methods with JUnit 5 | RESTful Web Services
Testing Spring Boot REST Controller Methods with JUnit 5 | RESTful Web Services

Spring Boot controllers allow you to extract request parameters and path variables using methods provided by the @RequestParam and @PathVariable annotations respectively.

Here's an example of a controller that accepts a name as a path variable and greet the user:

```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingsController { @GetMapping("/greet/{name}") public String greet(@PathVariable String name) { return "Hello, " + name + "!"; } } ``` In this example, the name passed in the URL like "/greet/John" will be used to greet the user.

Spring Boot + Thymeleaf: The Complete Guide (Build Your Web App!)
Spring Boot + Thymeleaf: The Complete Guide (Build Your Web App!)

Handling HTTP Requests and Responses

Spring Boot controllers can handle various HTTP methods (GET, POST, PUT, DELETE, etc.) with different annotations. They can also convert complex Java objects to and from JSON using the popular Jackson library.

spring boot tutorial class 4
spring boot tutorial class 4
Spring Boot Tutorial: Build an MVC Java Web App using Netbeans – Djamware
Spring Boot Tutorial: Build an MVC Java Web App using Netbeans – Djamware
the spring boot - mapping / error to a custom controller by implementing errorcontroler
the spring boot - mapping / error to a custom controller by implementing errorcontroler
Dependency Injection & Inversion of Control in Spring Tutorial #DI #IOC #Spring #SpringBoot #Java
Dependency Injection & Inversion of Control in Spring Tutorial #DI #IOC #Spring #SpringBoot #Java
Spring Boot Docker- How To Deploy Spring Boot Application In Docker?
Spring Boot Docker- How To Deploy Spring Boot Application In Docker?
Spring Boot MVC & REST Annotations With Examples
Spring Boot MVC & REST Annotations With Examples
a block diagram showing the process for building a web application with j3p pages
a block diagram showing the process for building a web application with j3p pages
2.2. Overview of Spring MVC Architecture — TERASOLUNA Global Framework Development Guideline 1.0.1.RELEASE documentation
2.2. Overview of Spring MVC Architecture — TERASOLUNA Global Framework Development Guideline 1.0.1.RELEASE documentation
Top 10 Free Core Spring, Spring MVC, and Spring Boot Courses for Beginners in 2026 - Best of Lot
Top 10 Free Core Spring, Spring MVC, and Spring Boot Courses for Beginners in 2026 - Best of Lot

Here's an example of a controller that handles CRUD operations for a simple "User" object:

User Controller

First, let's create a simple User class:

```java public class User { private Long id; private String name; // getters and setters } ``` Now, let's create a controller that handles CRUD operations for this User object:

```java import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; @RestController public class UserController { private ConcurrentHashMap users = new ConcurrentHashMap<>(); private AtomicLong counter = new AtomicLong(); @GetMapping("/users") public List getAllUsers() { return new ArrayList<>(users.values()); } @PostMapping("/users") public User addUser(@RequestBody User user) { user.setId(counter.incrementAndGet()); users.put(user.getId(), user); return user; } // other CRUD operations like getUser, updateUser, deleteUser can be implemented in a similar fashion } ``` In this example, the @RequestBody annotation is used to bind the HTTP request body to a User object. The @ResponseBody annotation is not explicitly used as it is already implied by the @RestController annotation.

Error Handling in Spring Boot Controllers

Spring Boot provides a mechanism for handling exceptions globally using @ControllerAdvice and @ExceptionHandler. This helps in centralizing error handling and providing consistent error responses.

Here's an example of global error handling for @ControllerAdvice:

```java import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; @ControllerAdvice public class GlobalExceptionHandler { @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) public String handleException(Exception e) { return e.getMessage(); } } ``` In this example, any exception thrown in a Spring Boot controller will be caught by the `handleException` method and return a 500 Internal Server Error response with the exception message.

That's it! You've now learned how to create and manage Spring Boot controllers, handle HTTP requests and responses, and even handle exceptions. Practice makes perfect, so go ahead and create your own controllers with more complex business logic. Happy coding!