liveBook · Manning

@Controller

As explained in Spring Start Here, Second Edition

The @Controller annotation marks a class as a Spring MVC controller component eligible for management by Spring.

@Controller marks a class as a Spring MVC controller. As a stereotype annotation like @Component or @Service, it lets Spring register the class as a managed bean. Its methods can handle specific HTTP requests. section 7.3

Listing 7.5 The definition of the controller class
@Controller    #A
public class MainController {
 
  @RequestMapping("/home")  #B
  public String home() {
    return "home.html";    #C
  }
}

A minimal controller maps /home to a method that returns home.html. section 7.3

For a regular controller, a method’s return value can name a view. To return a REST response body directly, use @ResponseBody or @RestController; otherwise, Spring treats the return value as a view name. section 10.6

How Spring Start Here, Second Edition covers it

How deep it goes
Defines bean registration and demonstrates a mapped action returning a view name, while deferring parameters, dynamic content, and alternative mappings.
Examples
Uses MainController to map /home to home.html and display a static welcome page.
What you can do after
Build a minimal Spring MVC request-response flow by marking a controller and mapping an action to a path.
How it connects
Connects Spring MVC and Spring Boot configuration with @Component, @Service, @RequestMapping, and @RestController.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.