![](/img/trans.png)
[英]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.