@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
