Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this tutorial, we’ll focus on a core concept in Spring MVC, Controllers.

2. Overview

Let’s start by taking a step back and examining the concept of the Front Controller in the typical Spring Model View Controller architecture.

At a very high level, the main responsibilities include:

  • intercepting incoming requests
  • converting the payload of the request to the internal structure of the data
  • sending the data to Model for further processing
  • getting processed data from the Model, and advancing that data to the View for rendering

Here’s a quick diagram for the high level flow in Spring MVC:

SpringMVC

As we can see, the DispatcherServlet plays the role of the Front Controller in the architecture.

The diagram is applicable to both typical MVC controllers, as well as RESTful controllers, with some small differences (described below).

In the traditional approach, MVC applications aren’t service-oriented; therefore, there’s a View Resolver that renders final views based on data received from a Controller.

RESTful applications are designed to be service-oriented and return raw data (JSON/XML, typically). Since these applications don’t do any view rendering, there are no View Resolvers, and the Controller is generally expected to send data directly via the HTTP response.

Let’s start with the MVC0-style controllers.

3. Maven Dependencies

In order to work with Spring MVC in Spring Boot, we’ll deal with the Maven dependencies first:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>3.0.2</version>
</dependency>

To get the latest version of the library, have a look at spring-boot-starter-web on Maven Central.

4. Spring Boot Web Config

Now let’s look at how we can configure the Spring Boot. Since we added the thymeleaf dependency in the classpath, we won’t need to configure any @Beans for that:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

In our WebConfig, we’ll need to add a bean for the Greeting object and the ObjectMapper in order to enable the default servlet:

    @Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> enableDefaultServlet() {
        return factory -> factory.setRegisterDefaultServlet(true);
    }

    @Bean
    public Greeting greeting() {
        Greeting greeting = new Greeting();
        greeting.setMessage("Hello World !!");
        return greeting;
    }

    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper();
    }

So, for example, if the Controller returns a view named “welcome”, the view resolver will try to resolve a page called “welcome.html” in the templates folder. This is the default folder where thymeleaf will search for views.

5. The MVC Controller

Now we’ll finally implement the MVC style controller.

Notice how we’re returning a ModelAndView object that contains a model map and a view object; both will be used by the View Resolver for data rendering:

@Controller
@RequestMapping(value = "/test")
public class TestController {

    @GetMapping
    public ModelAndView getTestData() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("welcome");
        mv.getModel().put("data", "Welcome home man");

        return mv;
    }
}

So what exactly did we set up here.

First, we created a controller called TestController and mapped it to the “/test” path. In the class, we created a method that returns a ModelAndView object, and is mapped to a GET request. Thus, any URL call ending with “test” will be routed by the DispatcherServlet to the getTestData method in the TestController.

And, of course, we’re returning the ModelAndView object with some model data for good measure.

The view object has a name set to “welcome“. As discussed above, the View Resolver will search for a page in the templates folder called “welcome.html“.

Below we can see the result of an example GET operation:

Welcome

6. The REST Controller

The setup for a Spring RESTful application is the same as the one for the MVC application, with the only difference being that there are no View Resolvers or model map.

The API will generally return raw data back to the client, XML and JSON representations usually, so the DispatcherServlet bypasses the view resolvers and returns the data right in the HTTP response body.

Let’s have a look at a simple RESTful controller implementation:

@RestController
public class RestController {

    @GetMapping(value = "/student/{studentId}")
    public Student getTestData(@PathVariable Integer studentId) {
        Student student = new Student();
        student.setName("Peter");
        student.setId(studentId);

        return student;
    }
}

We can see a quick snapshot of the output below:

Student Endpoint

The above output is a result of sending the GET request to the API with the student id of 1.

One quick note here is that the @RequestMapping annotation is one of those central annotations that we’ll really have to explore in order to use to its full potential.

7. Conclusion

In this article, we explored the basics of using controllers in Spring Boot, both from the point of view of a typical MVC application, as well as a RESTful API.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW