Java HTTP 状态 405 - Rest API 的方法不允许错误

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

HTTP Status 405 - Method Not Allowed Error for Rest API

javarest

提问by user2821894

Am asking this question after doing some research. I did followed the solutions given for this kind of error but did not work for me. Any suggestions as where am going wrong in the below code.I am creating a REST API but when I request the url it is giving me the 405 error.Below is the URI am requesting.

我在做了一些研究后问了这个问题。我确实遵循了针对此类错误给出的解决方案,但对我不起作用。关于以下代码中哪里出错的任何建议。我正在创建一个 REST API,但是当我请求 url 时,它给了我 405 错误。下面是我请求的 URI。

    http://localhost:8080/Project/services/start/version

Below is the code snippet.

下面是代码片段。

@Path("/start")

public class StartService {
@GET
@Path("/version")
@Produces({"text/plain","application/xml","application/json"})
public String getVersion() {
    String ver="";

    try{


          Runtime rt = Runtime.getRuntime();
          Process pr = rt.exec("C:\server\dgr -v" );

          BufferedReader stdInput = new BufferedReader(new InputStreamReader
(pr.getInputStream()));
          BufferedReader input = new BufferedReader(stdInput);
         // String ver ="";
          StringBuffer verOutput = new StringBuffer();
                while((ver =  input.readLine()) != null){
                    verOutput.append(ver + "\n");
                    System.out.println(ver);
                }


        }catch (Throwable t)  
          {  
            t.printStackTrace();  
          }  


        finally {  

        }
    return ver;  }

}

web.xml:

网页.xml:

<web-app 
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<servlet>
<display-name>eLicensingWeb</display-name>      
    <servlet-name>JAX-RS REST</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.cem.plc.service</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>




<servlet-mapping>
    <servlet-name>JAX-RS REST</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>


<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>


</web-app>

采纳答案by user2721787

@Produces({"text/plain","application/xml","application/json"})change this to @Produces("text/plain")and try,

@Produces({"text/plain","application/xml","application/json"})将此更改为@Produces("text/plain")并尝试,

回答by Gene

Add

添加

@Produces({"image/jpeg,image/png"})

to

@POST
@Path("/pdf")
@Consumes({ MediaType.MULTIPART_FORM_DATA })
@Produces({"image/jpeg,image/png"})
//@Produces("text/plain")
public Response uploadPdfFile(@FormDataParam("file") InputStream fileInputStream,@FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception {
    ...
}

回答by Nadine

I also had this problem and was able to solve it by enabling CORS support on the server. In my case it was an Azure server and it was easy: Enable CORS on Azure

我也遇到了这个问题,并且能够通过在服务器上启用 CORS 支持来解决它。就我而言,它是一个 Azure 服务器,很简单:在 Azure 上启用 CORS

So check for your server how it works and enable CORS. I didn't even need a browser plugin or proxy :)

因此,请检查您的服务器的工作方式并启用 CORS。我什至不需要浏览器插件或代理:)

回答by Girish

In above code variable "ver" is assign to null, print "ver" before returning and see the value. As this "ver" having null service is send status as "204 No Content".

在上面的代码变量“ver”被赋值为null,在返回之前打印“ver”并查看值。由于此具有空服务的“ver”发送状态为“204 No Content”。

And about status code "405 - Method Not Allowed" will get this status code when rest controller or service only supporting GET method but from client side your trying with POST with valid uri request, during such scenario get status as "405 - Method Not Allowed"

关于状态代码“405 - Method Not Allowed”将在其余控制器或服务仅支持 GET 方法但从客户端尝试使用有效 uri 请求进行 POST 时获得此状态代码,在这种情况下获得状态为“405 - Method Not Allowed” ”