liveBook · Manning

@getmapping

As explained in Spring in Action, Sixth Edition

@GetMapping is a Spring MVC annotation that maps HTTP GET requests to a controller method.

@GetMapping connects an HTTP GET request to a Spring MVC controller method. The annotation can map a path directly, or refine a class-level path to create the complete route. section 1.3.1 section 2.1.2

A GET-mapped method commonly performs these steps:

  • Receives a request for its mapped path. section 1.3.1
  • Prepares model data when the page needs it. section 2.1.2
  • Returns a logical view name, such as "home" or "taco", for rendering in the browser. section 1.3.1 section 2.1.2
  • Works alongside related annotations such as @RequestMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping. section 2.1.2

Mapping a home page

Place @GetMapping("/") on a controller method to handle a GET request for the root path. Returning "home" selects the logical home view; with Thymeleaf available, the corresponding template is /templates/home.html. section 1.3.1

Listing 1.4 The home page controller
package tacos;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
 
@Controller                     #1
public class HomeController {
 
  @GetMapping("/")              #2
  public String home() {
    return "home";              #3
  }
 
}

See the complete HomeController listing, including the controller annotation, root-path mapping, and returned view name. section 1.3.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains direct and combined path mapping, view-name returns, and model preparation without descending into framework internals.
Examples
Uses a home-page controller and a taco-design form mapped through class-level and method-level paths.
What it assumes
Requires a Spring MVC controller and basic HTTP GET knowledge while introducing annotation-based request handling.
What you can do after
Apply it via Spring MVC to connect GET URLs with controller methods and rendered logical views.
How it connects
Connects @GetMapping with Spring MVC controllers, @RequestMapping, model attributes, HTTP methods, and view templates.

Related concepts

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