@RequestParam不同的URI

时间:2019-02-12 23:52:21

标签: java spring

@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();
}

我不明白为什么帖子中只有一个正确的URI @PostMapping("recipe"),而当我尝试使用@PostMapping("/recipe/new")时却不起作用

我认为我不太了解在@Mappings中编写适当的URI的规则,为什么只有@PostMapping("recipe")有效?'

正常的控制器不能休息

1 个答案:

答案 0 :(得分:1)

  

Spring将不带method属性的@RequestMapping URI视为   多个映射,例如POST: /recipe/new , GET: /recipe/new , PATCH: /recipe/new ..

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

要解决此问题,请将request方法属性添加到您的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();
    }
相关问题