请求Web服务的发布请求返回HTTP状态405 - 方法不允许

时间:2017-06-03 10:39:29

标签: web-services rest http jersey http-error

我制作了一个有三个路径参数的POST网络服务。

但是当我尝试通过url请求此Web服务时,我收到HTTP状态405 - 方法不允许

但是如果我将Web服务更改为@GET

,同样的方法也可以
@POST
@Path("/authCode/{code}/{token}/{secret}")
public Response getToken(@PathParam("code") String code,@PathParam("token") String token,@PathParam("secret") String secret) {

    String output = code;
    System.out.println("code output"+output);

    System.out.println("********A basic user profile call into a subresource return data in JSON********"); //$NON-NLS-1$
    String url = "http://api.linkedin.com/v1/people/~/summary";

请求网址:

http://localhost:8080/SocialNetwork/rest/linkedin/authCode/17842/81--sdfsdf-8a57-adfd-9ddb-dfdddfdf/sdfsdfsd-fb54-402d-9a85-dsfdsfsdf

这是我得到的回复

<html>
<head>
    <title>Apache Tomcat/7.0.78 - Error report</title>
    <style>
        <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
    </style>
</head>
<body>
    <h1>HTTP Status 405 - Method Not Allowed</h1>
    <HR size="1" noshade="noshade">
    <p>
        <b>type</b> Status report
    </p>
    <p>
        <b>message</b>
        <u>Method Not Allowed</u>
    </p>
    <p>
        <b>description</b>
        <u>The specified HTTP method is not allowed for the requested resource.</u>
    </p>
    <HR size="1" noshade="noshade">
    <h3>Apache Tomcat/7.0.78</h3>
</body>

任何帮助都将不胜感激!!

1 个答案:

答案 0 :(得分:0)

这是因为服务器不允许使用POST方法。仅允许GET个方法。 405方法不允许表示此。

要使POST请求生效,您需要在服务器上的HTTP请求的标头中启用POST请求方法。您通常可以通过为此类呼叫添加端点来执行此操作。

例如,对于GET请求,您的代码可能如下所示:

router.GET('/some-endpoint', someGETHandlerFunc)

要为POST启用相同的端点,您需要添加:

router.POST('/some-endpoint', somePOSTHandlerFunc)

合:

router.GET('/some-endpoint', someGETHandlerFunc)
router.POST('/some-endpoint', somePOSTHandlerFunc)

现在问你的问题......

您的请求端点与POST端点不匹配。

问题中指出的POST端点是:

/authCode/{code}/{token}/{secret}

您提供的请求网址与之匹配,因为它前面有/SocialNetwork/rest/linkedin/。除非您的服务器端点中的装饰器也预先设置了这个,否则可能是您的错误。