Featured Article

Spring Boot MVC Controller Example: A Complete Guide

Kenneth Jul 13, 2026

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.

Spring Boot MVC REST Annotations With Examples - JavaTechOnline
Spring Boot MVC REST Annotations With Examples - JavaTechOnline

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`:

Spring MVC Interceptor with Example - Dinesh on Java
Spring MVC Interceptor with Example - Dinesh on Java

```xml org.springframework.boot spring-boot-starter-web ```

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

Spring Boot MongoDB Java Tutorial With Examples - JavaTechOnline
Spring Boot MongoDB Java Tutorial With Examples - JavaTechOnline

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

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

`@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

How to return different HTTP status code from a Spring MVC controller? Example Tutorial
How to return different HTTP status code from a Spring MVC controller? Example Tutorial

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

How to validate HTTP POST Request Payload on Spring MVC controller? Example Tutorial
How to validate HTTP POST Request Payload on Spring MVC controller? Example Tutorial

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`:

Spring Boot Real-Time Project
Spring Boot Real-Time Project
How to create a Java Web application using Spring MVC Framework? Example Tutorial
How to create a Java Web application using Spring MVC Framework? Example Tutorial
Spring MVC Hello World Example
Spring MVC Hello World Example
Spring Boot cheatsheet - Masterspringboot
Spring Boot cheatsheet - Masterspringboot
Top 10 Projects You can Build to Learn Spring Boot in 2025
Top 10 Projects You can Build to Learn Spring Boot in 2025
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
What is @Controller and @RestController annotations in Spring MVC? Example and Differences
What is @Controller and @RestController annotations in Spring MVC? Example and Differences
Spring Boot Folder Structure: Best Practices and Guide
Spring Boot Folder Structure: Best Practices and Guide
Login and Signup with Java and Spring Boot
Login and Signup with Java and Spring Boot

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