java Spring Boot 控制器不支持请求方法“GET”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46854250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Request method 'GET' not supported in Spring Boot Controller
提问by Saurabh
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/updateand click on SubmitI call the @PostMappingmethod which upon updating redirects me to
return "redirect:/recipe/" + savedCommand.getId() + "/show";
现在,当我转到http://localhost:8080/recipe/2/update并单击提交时,我调用@PostMapping方法,该方法在更新时会将我重定向到
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
当我将 @PostMapping 更改为 @RequestMapping 或添加额外的 @GetMaapping 时,一切正常
Can any one explain this or what can i do so that @PostMapping works as expected.
任何人都可以解释这一点,或者我能做些什么才能使@PostMapping 按预期工作。
UPDATE: as mentioned in the comments below - we can directly use @PathVariable in SpringData https://stackoverflow.com/a/39871080/4853910
更新:如下面的评论中提到的 - 我们可以直接在 SpringData https://stackoverflow.com/a/39871080/4853910 中使用 @PathVariable
采纳答案by Oliver Erdmann
I guess you have to change the FORM in the HTML so that it uses POST on submit, not GET: Do it this way
我猜您必须更改 HTML 中的 FORM,以便它在提交时使用 POST,而不是 GET:这样做
<form method="post" ...>
<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).
至少屏幕截图似乎显示了提交 HTML FORM 后来自浏览器的提交请求,并且在那里显示它是一个 GET 请求(所有表单字段作为请求参数)。
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.
所以 spring 是正确的:URL ("/recipe/?id=2&description=Spicy...") 只匹配 saveAndUpdate() 的 Mapping,对于这个方法,你只注释了“POST”,因此:没有匹配在您的控制器中获取“/recipe/?id=2&description=Spicy...”。
Can you post here the HTML snippet with the
<FORM>...</FORM>
part?
你能在这里发布带有<FORM>...</FORM>
零件的 HTML 片段
吗?