concept @PostMapping in category spring

appears as: @PostMapping
Spring in Action, Fifth Edition

This is an excerpt from Manning's book Spring in Action, Fifth Edition.

Generally, I prefer to only use @RequestMapping at the class level to specify the base path. I use the more specific @GetMapping, @PostMapping, and so on, on each of the handler methods.

In listing 2.2, you used the @GetMapping annotation to specify that the showDesignForm() method should handle HTTP GET requests for /design. Just like @GetMapping handles GET requests, you can use @PostMapping to handle POST requests. For handling taco design submissions, add the processDesign() method in the following listing to DesignTacoController.

Listing 2.4. Handling POST requests with @PostMapping
@PostMapping
public String processDesign(Taco design) {
  // Save the taco design...
  // We'll do this in chapter 3
  log.info("Processing design: " + design);

  return "redirect:/orders/current";
}

As applied to the processDesign() method, @PostMapping coordinates with the class-level @RequestMapping to indicate that processDesign() should handle POST requests for /design. This is precisely what you need to process a taco artist’s submitted creations.

  • Spring MVC is annotation-based, enabling the declaration of request-handling methods with annotations such as @RequestMapping, @GetMapping, and @PostMapping.