Spring Boot Controller不支持请求方法'GET' [英] Request method 'GET' not supported in Spring Boot Controller
问题描述
我有以下代码
@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)));
返回recipe / show;
}
@GetMapping(/ {id} / update)
public String updateRecipe(@PathVariable String id,Model model){
model.addAttribute(recipe, recipeService.findCommandById(Long.valueOf(ID)));
返回recipe / recipeform;
}
@PostMapping(/)
public String saveOrUpdate(@ModelAttribute RecipeCommand command){
RecipeCommand savedCommand = recipeService.saveRecipeCommand(command);
返回redirect:/ recipe /+ savedCommand.getId()+/ show;
}}
现在我去
更新:如下面的评论所述 - 我们可以在SpringData $ b $中直接使用@PathVariable b https://stackoverflow.com/a/39871080/4853910
我想你必须更改HTML中的FORM,以便它在提交时使用POST,而不是GET:这样做
< form method =post...>
至少截图似乎在提交HT后显示来自浏览器的提交请求ML FORM,
并且它显示它是一个GET请求(所有表单字段作为请求参数)。
所以spring是正确的:URL( / recipe /?id = 2& description = Spicy ...)仅匹配saveAndUpdate()的映射,对于此方法,您只注释了POST,因此:/ recipe /上的GET没有匹配在您的控制器中完全是?id = 2& description = Spicy ......。
你能在这里发布带有
<$ c $的HTML片段吗? c>< FORM> ...< / FORM>
part?
I have the following code
@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";
}}
Now when i go to http://localhost:8080/recipe/2/update and click on Submit I call the @PostMapping method which upon updating redirects me to
return "redirect:/recipe/" + savedCommand.getId() + "/show";
But then i get this error on the console
Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
and this on the web
There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported
When i change the @PostMapping to @RequestMapping or add additional @GetMaapping everything works
Can any one explain this or what can i do so that @PostMapping works as expected.
Possible Reason
UPDATE: as mentioned in the comments below - we can directly use @PathVariable in SpringData https://stackoverflow.com/a/39871080/4853910
I guess you have to change the FORM in the HTML so that it uses POST on submit, not GET: Do it this way
<form method="post" ...>
At least the screenshot seems to show the submit request from the browser after submitting the HTML FORM, and it shows there that it is a GET request (with all form fields as request parameters).
So spring ist right: the URL ("/recipe/?id=2&description=Spicy...") only matches the Mapping of saveAndUpdate(), and for this method you only annotated "POST", hence: there is no matching for GET on "/recipe/?id=2&description=Spicy..." in your Controller at all.
Can you post here the HTML snippet with the
<FORM>...</FORM>
part?
这篇关于Spring Boot Controller不支持请求方法'GET'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!