SPRING CONCEPTS

Controller

Spring Concepts / Controller

A controller translates HTTP requests into application calls and responses.

Understand the idea

@Controller commonly returns view names and model data. @RestController combines controller behavior with response-body semantics, so returned objects are serialized. Route annotations select HTTP methods and paths. Business operations belong in services rather than growing inside route methods.

A concrete example

@GetMapping("/todos")
String list(Model model) {
    model.addAttribute("todos", service.list());
    return "list";
}

In the application

TodoController renders HTML; TodoRestController and PersonalTodoController return API responses.

Returning "list" from an MVC controller selects a template; returning a String from a REST controller generally sends response text.

Detailed walkthrough: Trace a Spring MVC request →

See it in a working stage

Related concepts

Read the official documentation ↗
Java 21 · Spring Boot 4.1.1 · Edupoly