In the dynamic world of web development, Spring Boot has emerged as a popular choice for creating robust and scalable applications. Among its many powerful features, the MVC (Model-View-Controller) architecture stands out, enabling a clean separation of concerns. Let's delve into a practical example of creating an MVC controller in Spring Boot.

Before we dive into the coding part, let's ensure we have the necessary setup. We'll need the Spring Web module in our project, as it contains the necessary classes for handling HTTP requests and responses. If you're using Maven, add this to your `pom.xml`:

```xml
Creating a Simple Controller
At the heart of Spring Boot's MVC architecture lies the controller. It's responsible for handling HTTP requests and generating appropriate responses. Let's create a simple controller named `GreetingController`.

This example will use a simple REST API. Here's how to create the controller:
```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { @GetMapping("/greet") public String greet() { return "Hello, World!"; } } ```
Understanding the Annotations

`@RestController` is an annotation that indicates this class functions as a controller. It's a shortcut for `@Controller` and `@ResponseBody`.
`@GetMapping` is a composite annotation consisting of `@RequestMapping(method = RequestMethod.GET)`. It signifies that the `greet()` method should handle HTTP GET requests mapped to the "/greet" URL.
Testing the Controller

To test our controller, we can use any REST client like Postman or curl. Here's how to use curl:
```bash $ curl -X GET "http://localhost:8080/greet" Hello, World! ```
As you can see, we've successfully created our first Spring Boot MVC controller!
Handling Path Variables

Next, let's enhance our controller to accept path variables. This will allow us to greet people by name.
Here's how to modify the `GreetingController`:









```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 GreetingController { @GetMapping("/greet/{name}") public String greet(@PathVariable String name) { return "Hello, " + name + "!"; } } ```
Exploring `@PathVariable`
The `@PathVariable` annotation is used to extract the value of a path variable, which is mapped to the method parameter. In this case, the value of the `{name}` placeholder will be injected into the `name` parameter.
Testing the updated controller with curl:
```bash $ curl -X GET "http://localhost:8080/greet/Java" Hello, Java! ```
That's how you handle path variables in a Spring Boot MVC controller.
Handling Query Parameters
Sometimes, you might want to pass additional information through query parameters. Let's modify our `GreetingController` to accept a `language` parameter to return greetings in different languages.
Here's how to achieve this:
```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { @GetMapping("/greet") public String greet(@RequestParam(value = "name", defaultValue = "World") String name, @RequestParam(value = "language", defaultValue = "en") String language) { return "Hello, " + name + " in " + language; } } ```
Introducing `@RequestParam`
The `@RequestParam` annotation is used to extract and bind request parameters to method parameters. In this example, both `name` and `language` parameters have default values that will be used if the respective parameters are not provided.
Testing the updated controller with curl:
```bash $ curl -X GET "http://localhost:8080/greet?name=Java&language=es" Hola, Java! ```
With this, we've explored different ways of handling path variables and query parameters in a Spring Boot MVC controller.
To conclude, Spring Boot's MVC architecture offers a clean and efficient way to handle HTTP requests and responses. By enthusiastically embracing this design pattern, developers can steadily build robust and maintainable web applications. Happy coding!