繁体   English   中英

带有JSON主体的POST REST API调用在Java的播放框架中不起作用

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM