简体   繁体   中英

POST REST API call with json body is not working in play framework with java

I am unable to call an API call in play framework with Java. the code looks like a good, but the API call was not triggering(API call is working fine in Postman). I think it might be the issue related to threads and I am unable to write the async task in play framework, please someone help me. Hoping the Best! Thanks in advance!

find the code of API calling in the controller file

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);
    }
}

Apparently, you are not using play commands for your HTTP request. Your code should be as follows:

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");
                            }
                        }
                );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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