[英]POST REST API call with json body is not working in play framework with java
我無法使用Java在播放框架中調用API調用。 代碼看起來不錯,但是API調用未觸發(在Postman中,API調用工作正常)。 我認為這可能是與線程相關的問題,我無法在play框架中編寫異步任務,請有人幫我。 希望最好! 提前致謝!
在控制器文件中找到API調用的代碼
public Result updateTripDetail() {
//few statements;
String url = "https://medicines-XXX/MZIMRestServicesXXX/v1/XXXX";
JSONObject jsonBody = new JSONObject();
jsonBody.put("source_type", "XXX");
jsonBody.put("omorder_id", "25852");
if (Rescheduled.equals(ride.getRideStatus())) {
jsonBody.put("order_status", "RequestForReschedule");
} else if (RideCancelled.equals(ride.getRideStatus())) {
jsonBody.put("order_status", "RequestForCancel");
} else {
jsonBody.put("order_status", ride.getRideStatus());
}
jsonBody.put("last_updated_on", new Date());
apiPostCall(url,jsonBody.toString());
return redirect("/ride/rideList");
}
public void apiPostCall(String completeUrl, String body) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(completeUrl);
httpPost.setHeader("Content-type", "application/json");
try {
StringEntity stringEntity = new StringEntity(body);
httpPost.getRequestLine();
httpPost.setEntity(stringEntity);
httpClient.execute(httpPost);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
顯然,您沒有在HTTP請求中使用播放命令。 您的代碼應如下所示:
public void updateTripDetail() {
//few statements;
String url = "https://medicines-XXX/MZIMRestServicesXXX/v1/XXXX";
JSONObject jsonBody = new JSONObject();
jsonBody.put("source_type", "XXX");
jsonBody.put("omorder_id", "25852");
if (Rescheduled.equals(ride.getRideStatus())) {
jsonBody.put("order_status", "RequestForReschedule");
} else if (RideCancelled.equals(ride.getRideStatus())) {
jsonBody.put("order_status", "RequestForCancel");
} else {
jsonBody.put("order_status", ride.getRideStatus());
}
jsonBody.put("last_updated_on", new Date());
apiPostCall(url,jsonBody.toString());
//As an asynchronous request it would
//probably redirect before completing your request so void it's okay
//return redirect("/ride/rideList");
}
public Promise<Result> apiPostCall(String completeUrl, String body) {
return WS.url(completeUrl)
.setHeader("Content-Type", "application/json")
.post(body).map(
new F.Function<WS.Response, Result>() {
public Result apply(WS.Response response) {
//you can handle your response here
//String token = response.asJson().findPath("access_token").asText();
return redirect("/ride/rideList");
}
}
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.