Search⌘ K
AI Features

Posting Data

Understand how to use Spring MVC controllers to post data using @PostMapping. Learn to handle form data, implement the Post/Redirect/Get pattern to avoid duplicate submissions, and explore support for all HTTP methods in Spring Boot for scalable application development.

Posting data

We saw how to use @GetMapping to display information to the user. Inevitably, the user will also want to change data. In web terms, we can do this by using HTTP POST with a <form>.

Suppose we have a form to change the name of a team. The controller method to make that possible could look something like this:

Java
@Controller
@RequestMapping("/teams")
public class TeamController {
...
@PostMapping("/{id}")
public String editTeamName(@PathVariable("id") String teamId,
@ModelAttribute("editTeamFormData") EditTeamFormDataformData) {
service.changeTeamName(teamId, formData.getTeamName());
return "redirect:/teams/all";
}
}
  • Use the @PostMapping annotation to indicate that a POST call to /teams/<id> should be handled by this method.
  • The editTeamFormData is
...