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'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.

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.

Here's a basic example of a Spring Boot controller:
Hello World Controller

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

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.

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.









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
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!