Java Rest Web 服务中请求的资源不允许指定的 HTTP 方法

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

The specified HTTP method is not allowed for the requested resource in Rest web services

javarestjax-rs

提问by Mayur Gupta

i am using restful webservices I have a simple code as given below:

我正在使用restful webservices 我有一个简单的代码,如下所示:

@Path("/v1/status")
public class ControllerServices 
{
    @GET
    @Produces(MediaType.TEXT_HTML)
    String printOnly()
    {
        System.out.println("running successfully");
        return "<p>this webservice</p>";
    }


}

And my web.xmlfile goes like this:

我的web.xml文件是这样的:

    <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.techbloomer.services</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
   <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

When I request

当我请求时

http://localhost:8080/webservicesForIndTadka/rest/v1/status

it gives the error as

它给出的错误为

HTTP Status 405 - Method Not Allowed
type: Status report
message: Method Not Allowed
description: The specified HTTP method is not allowed for the requested resource.

采纳答案by Mayur Gupta

@Path("/v1/status")
public class ControllerServices 
{
    @GET
    @Path("print")
    @Produces(MediaType.TEXT_HTML)
    public String printOnly()
    {
        System.out.println("running successfully");
        return "<p>this webservice</p>";
    }
}

The path annotation on the class indicates that this is a root resource class and the path value given specifies the base URI for all the web service methods contained in the class.

类上的路径注释表明这是一个根资源类,并且给定的路径值指定了该类中包含的所有 Web 服务方法的基本 URI。

The @GET annotation is used to differentiate between a sub-resource method that handles the actual web service request and a sub-resource locator method that returns an object that will instead be used to handle the request. In this case, the method has the @GET annotation which means this method handles the request and returns the result.

@GET 注释用于区分处理实际 Web 服务请求的子资源方法和返回将用于处理请求的对象的子资源定位器方法。在这种情况下,该方法具有 @GET 注释,这意味着该方法处理请求并返回结果。

If you navigate to http://localhost:8080/webservicesForIndTadka/rest/v1/status/print/you should see printOnlyreturning "<p>this webservice</p>".

如果您导航到http://localhost:8080/webservicesForIndTadka/rest/v1/status/print/您应该看到printOnly返回"<p>this webservice</p>".

Use FireFox and install RESTClient

使用 FireFox 并安装RESTClient

回答by ren78min

I've seen a similar problem, when a GET request has a Content-Type in header, tomcat (version 7 in my case) returns error code 405.

我见过类似的问题,当 GET 请求在标头中有 Content-Type 时,tomcat(在我的情况下为版本 7)返回错误代码 405。

回答by dendini

  1. Your browser is sending a different content-type to the JAX-RS service.
  1. 您的浏览器正在向 JAX-RS 服务发送不同的内容类型。

Try

尝试

@Produces(
    {
        MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
    })
  1. You should add
  1. 你应该添加

the following

下列

@Path("/get")

for specifying the path to the method, JAX-RS may be using something different.

为了指定方法的路径,JAX-RS 可能使用了不同的东西。

回答by ralgrad

http://localhost:8080/webservicesForIndTadka/rest/v1/status

I just had a similar problem and it boiled down to a missing / at the and of the request. In this case it would be

我刚刚遇到了类似的问题,归结为请求中缺少 / 。在这种情况下,它将是

http://localhost:8080/webservicesForIndTadka/rest/v1/status/

回答by Manoj Krishna

hi i have encountered the same problem but fixed it.

嗨,我遇到了同样的问题,但已解决。