Java spring Boot -"status":405,"error":"Method Not Allowed"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34965192/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:11:00  来源:igfitidea点击:

spring Boot -"status":405,"error":"Method Not Allowed"

javaspring-boot

提问by Yogesh Bedekar

I created a simple Spring Boot REST service using the guide, and am getting following error when I POST to http://localhost:8080/greeting

我使用指南创建了一个简单的 Spring Boot REST 服务,当我发布到http://localhost:8080/greeting

-"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported",

-"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"不支持请求方法'POST'",

I didn't find any info on how to solve the error. I am using version 1.3.2.RELEASE and starting Tomcat using mvn spring-boot:run.

我没有找到有关如何解决错误的任何信息。我正在使用 1.3.2.RELEASE 版本并使用mvn spring-boot:run.

The guide says that all HTTP methods are allowed by default. So why does it respond that POST is not supported?

该指南说默认情况下允许所有 HTTP 方法。那么为什么它会响应不支持 POST 呢?

How can I get Spring to invoke the controller method when I POST to this URL?

当我发布到这个 URL 时,如何让 Spring 调用控制器方法?

回答by whistling_marmot

As it says in the Spring REST guide,

正如Spring REST 指南中所说,

@RequestMappingmaps all HTTP operations by default

@RequestMapping默认映射所有 HTTP 操作

but if, as they suggest, you added a specification of the allowable http methods:

但是,如果按照他们的建议,您添加了允许的 http 方法的规范:

@RequestMapping(method=GET)

@RequestMapping(method=GET)

then only GETs will be allowed. POSTs will be disallowed.

那么只允许 GET。POST 将被禁止。

If you want to allow both GET and POST, but disallow all other http methods, then annotate your controller method thusly:

如果您想同时允许 GET 和 POST,但不允许所有其他http 方法,那么这样注释您的控制器方法:

@RequestMapping(value = "/greeting", method = {RequestMethod.GET, RequestMethod.POST})
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, name));
}

When you start the application, all the request handler mappings are logged out. You should see a line like this in your log (in the IDE console or command line window):

当您启动应用程序时,所有请求处理程序映射都将被注销。您应该在日志中看到这样的一行(在 IDE 控制台或命令行窗口中):

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting],methods=[GET || POST]}" onto public hello.Greeting hello.GreetingController.greeting(java.lang.String)

swsmmaRequestMappingHandlerMapping :将“{[/greeting],methods=[GET || POST]}”映射到 public hello.Greeting hello.GreetingController.greeting(java.lang.String)

Also, how are you POSTing? I can recommend the Advanced REST clientfor Google Chrome. If you're posting from a form in your browser, try hitting f12 and examining the Network tab to check that you are POSTing exactly what you think you're POSTing.

另外,你是怎么发帖的?我可以为 Google Chrome推荐高级 REST 客户端。如果您从浏览器中的表单发布,请尝试按 f12 并检查网络选项卡以检查您发布的内容是否与您认为的内容完全一致。

POSTing to the wrong URL, e.g. http://localhost:8080/greetingsinstead of http://localhost:8080/greetingwill result in a Method Not Allowederror, although really the error is a typo in the URL, not the choice of HTTP method.

POST 到错误的 URL,例如,http://localhost:8080/greetings而不是http://localhost:8080/greeting将导致Method Not Allowed错误,尽管该错误实际上是 URL 中的拼写错误,而不是 HTTP 方法的选择。

To see more detailed logging for the request processing, create a src/main/resourcesfolder alongside your src/main/javafolder, and create an application.propertiesfile in there. Add this line to that file

要查看请求处理的更详细日志记录,请在src/main/java文件夹旁边创建一个src/main/resources文件夹,并在其中创建一个application.properties文件。将此行添加到该文件

logging.level.org.springframework.web=DEBUG

Then, if you try to POST to an unmapped URL, e.g. /greetingsinstead of /greeting, you'll see these lines in you log:

然后,如果您尝试 POST 到未映射的 URL,例如,/greetings而不是/greeting,您将在日志中看到这些行:

RequestMappingHandlerMapping : Looking up handler method for path /greetings RequestMappingHandlerMapping : Did not find handler method for [/greetings]

RequestMappingHandlerMapping :查找路径 /greetings 的处理程序方法 RequestMappingHandlerMapping :未找到 [/greetings] 的处理程序方法