@RequestMapping associates an HTTP request pattern with a Spring MVC controller or handler method. Applied to a controller class, it establishes a shared path prefix for the requests that controller handles. For example, @RequestMapping("/design") makes DesignTacoController responsible for requests beginning with /design. section 2.1.2
Mapping a taco design controller
A controller can combine a class-level @RequestMapping with method-level mappings. With @RequestMapping("/design") on DesignTacoController and @GetMapping on showDesignForm(), Spring MVC calls that method for an HTTP GET request to /design. The method can populate the model and return "taco", the logical view name rendered for the browser. section 2.1.2
Use the mapping annotation that expresses the required HTTP operation:
- @RequestMapping — general-purpose request handling. section 2.1.2
- @GetMapping — handles HTTP GET requests. section 2.1.2
- @PostMapping — handles HTTP POST requests. section 2.1.2
- @PutMapping, @DeleteMapping, and @PatchMapping handle their corresponding HTTP methods. section 2.1.2
Listing 2.4 The beginnings of a Spring controller class
package tacos.web;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import lombok.extern.slf4j.Slf4j;
import tacos.Ingredient;
import tacos.Ingredient.Type;
import tacos.Taco;
import tacos.TacoOrder
@Slf4j
@Controller
@RequestMapping("/design")
@SessionAttributes("tacoOrder")
public class DesignTacoController {
@ModelAttribute
public void addIngredientsToModel(Model model) {
List<Ingredient> ingredients = Arrays.asList(
new Ingredient("FLTO", "Flour Tortilla", Type.WRAP),
new Ingredient("COTO", "Corn Tortilla", Type.WRAP),
new Ingredient("GRBF", "Ground Beef", Type.PROTEIN),
new Ingredient("CARN", "Carnitas", Type.PROTEIN),
new Ingredient("TMTO", "Diced Tomatoes", Type.VEGGIES),
new Ingredient("LETC", "Lettuce", Type.VEGGIES),
new Ingredient("CHED", "Cheddar", Type.CHEESE),
new Ingredient("JACK", "Monterrey Jack", Type.CHEESE),
new Ingredient("SLSA", "Salsa", Type.SAUCE),
new Ingredient("SRCR", "Sour Cream", Type.SAUCE)
);
Type[] types = Ingredient.Type.values();
for (Type type : types) {
model.addAttribute(type.toString().toLowerCase(),
filterByType(ingredients, type));
}
}
@ModelAttribute(name = "tacoOrder")
public TacoOrder order() {
return new TacoOrder();
}
@ModelAttribute(name = "taco")
public Taco taco() {
return new Taco();
}
@GetMapping
public String showDesignForm() {
return "design";
}
private Iterable<Ingredient> filterByType(
List<Ingredient> ingredients, Type type) {
return ingredients
.stream()
.filter(x -> x.getType().equals(type))
.collect(Collectors.toList());
}
}
Inspect a complete controller structure showing @RequestMapping alongside @GetMapping, @PostMapping, model handling, and session state. section 2.1.2
