Spring Boot 控制器不支持请求方法“GET”

Ran*_*urn 5 java spring spring-boot

我有以下代码

@Controller
@RequestMapping("/recipe")
public class RecipeController {
private IRecipeService recipeService;

@Autowired
public RecipeController(IRecipeService recipeService) {
    this.recipeService = recipeService;
}

@GetMapping("/{id}/show")
public String showById(@PathVariable String id, Model model) {

    model.addAttribute("recipe", recipeService.findById(Long.valueOf(id)));

    return "recipe/show";
}
@GetMapping("/{id}/update")
    public String updateRecipe(@PathVariable String id, Model model) {
        model.addAttribute("recipe", recipeService.findCommandById(Long.valueOf(id)));

    return "recipe/recipeform";
}

@PostMapping("/")
public String saveOrUpdate(@ModelAttribute RecipeCommand command) {
    RecipeCommand savedCommand = recipeService.saveRecipeCommand(command);

    return "redirect:/recipe/" + savedCommand.getId() + "/show";
}}
Run Code Online (Sandbox Code Playgroud)

现在,当我转到http://localhost:8080/recipe/2/update并单击提交时,我调用@PostMapping方法,该方法在更新时会将我重定向到 return "redirect:/recipe/" + savedCommand.getId() + "/show";

但是后来我在控制台上收到此错误

Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
Run Code Online (Sandbox Code Playgroud)

这在网上

There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported
Run Code Online (Sandbox Code Playgroud)

当我将 @PostMapping 更改为 @RequestMapping 或添加额外的 @GetMaapping 时,一切正常

任何人都可以解释这一点,或者我能做些什么来使@PostMapping 按预期工作。

可能的原因 样本

更新:如下面的评论中提到的 - 我们可以在 SpringData /sf/answers/2790975631/ 中直接使用 @PathVariable