[英]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.