gpt4 book ai didi

java - @RequestParam不同的URI

转载 作者:行者123 更新时间:2023-12-02 10:16:19 26 4
gpt4 key购买 nike

@RequestMapping("/recipe/new")
public String newRecipe(Model model){

model.addAttribute("recipe",new RecipeCommand());
return "recipe/recipeform";
}

@PostMapping("recipe")
public String saveOrUpdate(@ModelAttribute RecipeCommand command){

RecipeCommand recipeCommand=recipeService.saveRecipeCommand(command);
return "redirect:/recipe/show/"+recipeCommand.getId();
}

我不明白为什么在 post 中只有一个正确的 URI @PostMapping("recipe") ,当我尝试例如 @PostMapping("/recipe/new") 时它不起作用

我想我不太明白在 @Mappings 中编写正确 URI 的规则,为什么只有 @PostMapping("recipe") 有效?'

这是正常的 Controller ,没有休息

最佳答案

Spring will consider @RequestMapping URI without method property as multiple mappings like POST: /recipe/new , GET: /recipe/new , PATCH:
/recipe/new ..

由于上述原因,@PostMapping("/recipe/new") 被视为重复映射,因此不起作用。

要解决此问题,请将请求方法属性添加到您的 newRecipe 方法

@RequestMapping("/recipe/new", method=RequestMethod.GET)
public String newRecipe(Model model){
model.addAttribute("recipe",new RecipeCommand());
return "recipe/recipeform";
}

@PostMapping("/recipe/new")
public String saveOrUpdate(@ModelAttribute RecipeCommand command){
RecipeCommand recipeCommand=recipeService.saveRecipeCommand(command);
return "redirect:/recipe/show/"+recipeCommand.getId();
}

关于java - @RequestParam不同的URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54660489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com