简体   繁体   English

使用Java Jersey API对REST API的发布请求获取错误405

[英]Getting Error 405 for post request to REST api using Java Jersey api

I wrote a method using Jersey API to make a post request to an api. 我编写了一个使用Jersey API的方法来向api发出发布请求。 The api requires the data being posted be in JSON format and has requires a Basic Authorization header however when I run the code below and pass the object it results in the following error api要求发布的数据为JSON格式,并且需要基本授权标头,但是当我运行以下代码并传递对象时,会导致以下错误

java.lang.RuntimeException: Failed : HTTP error code : 405
at com.shumbamoney.yomoney.SendRequest.send(SendRequest.java:40)

.The java code is below. Java代码如下。

 public String send(TransactionRequestObject tRObject){

    Gson gson = new Gson();
    Gson gsonBuilder = new GsonBuilder().create();
    String jsonRObject = gsonBuilder.toJson(tRObject);


    ApiCredentials credentials = new ApiCredentials();
    postUrl = credentials.getURL();
    AgentCode = credentials.getAgentCode();
    Password = credentials.getPassword();
    System.out.println(jsonRObject);

   // jersey code
    try{
        Client client = Client.create();
        WebResource webResource = client.resource(postUrl);
        ClientResponse response = webResource.type("application/json; charset=ISO-8859-1").header(HttpHeaders.AUTHORIZATION, "Basic "+
                AgentCode+":"+Password).post(ClientResponse.class, jsonRObject);
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }

        System.out.println("Output from Server .... \n");
        String output = response.getEntity(String.class);
        System.out.println(output);
    }catch(Exception e){
        e.printStackTrace();
    }

    return "success";
}

Your help is greatly appreciated. 非常感谢您的帮助。

your code did all the things it should: send the request and get the response from the API, so the error doesn't come from your code, it's from the server that you are requesting to. 您的代码完成了它应该做的所有事情:发送请求并从API获取响应,因此错误不是源于您的代码,而是源于您请求的服务器。
try using postman to POST to that url, if you still get the 405 error, than you can make sure that the problem is not from ur code. 如果仍然收到405错误,请尝试使用邮递员将其发布到该网址,然后可以确保问题不是来自您的代码。

Thank you everyone for your contributions I really appreciate them. 谢谢大家的贡献,我非常感谢他们。 It turns out the issue was on the server that I was requesting to. 原来问题出在我请求的服务器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用POST与@FormParam(带有Jersey REST的Java Web服务),遇到405“方法不允许”错误 - Getting 405 “Method Not Allowed” error using POST with @FormParam (Java web service with Jersey REST) REST API | 球衣罐| 405错误 - REST API | Jersey jar | 405 error 您如何使用 jersey REST Webtarget API 发布对象的 json 列表? - 未找到错误 MessageBodyWriter - How do you POST a json list of objects using the jersey REST Webtarget API? - getting error MessageBodyWriter not found 在Java中将状态405发送到api - getting status 405 in java post to api 泽西邮政405错误的Json消费 - Jersey Post Request 405 Error for Json Consumption java rest API post方法提供了不允许的HTTP 405方法 - java rest API post method gives HTTP 405 method not allowed 使用Jersey 2的REST API - REST API using Jersey 2 Java REST API 发送 GET 请求时出错,但在 POST 上工作 - Java REST API Getting Error when sending GET Request, but works on POST CRUD在休眠状态下从ManyToMany中使用Jersey使用Java的REST API在REST API中使用POST方法读取JSON的问题 - CRUD issue reading a JSON using POST method in a REST API in Java using Jersey from a ManyToMany noted in hibernate 使用REST API和Java Jersey应用程序在Wordpress上发布文章 - Publish post on Wordpress with REST API and Java Jersey application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM