[英]Getting 405 “Method Not Allowed” error using POST with @FormParam (Java web service with Jersey REST)
[英]Getting Error 405 for post request to REST api using Java Jersey api
我编写了一个使用Jersey API的方法来向api发出发布请求。 api要求发布的数据为JSON格式,并且需要基本授权标头,但是当我运行以下代码并传递对象时,会导致以下错误
java.lang.RuntimeException: Failed : HTTP error code : 405
at com.shumbamoney.yomoney.SendRequest.send(SendRequest.java:40)
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";
}
非常感谢您的帮助。
您的代码完成了它应该做的所有事情:发送请求并从API获取响应,因此错误不是源于您的代码,而是源于您请求的服务器。
如果仍然收到405错误,请尝试使用邮递员将其发布到该网址,然后可以确保问题不是来自您的代码。
谢谢大家的贡献,我非常感谢他们。 原来问题出在我请求的服务器上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.