Lorenzo Sogliani
Lorenzo Sogliani

Reputation: 227

Call RestFull Web Service inside Servlet

i have a standard HttpServlet in my java web project. I use Netbeans. I want to call a Restfull Web service inside servlet and after i will catch the response like a JSON and populate a JSP. I tried to find on the net but i didn't find anything.

Thank you

Upvotes: 0

Views: 79

Answers (1)

Dr Cox
Dr Cox

Reputation: 159

Here's an example of HttpPost:

    try {
            HttpPost httpPost = new HttpPost("https://exampleurl/providerexample/api/v1/loansforexample"
            );
            StringEntity params;
            params = new StringEntity("{"
                    + "\"clientId\": \"" + "2" + "\","
                    + "\"productId\": \"" + "1" + "\"," 
                    + "\"locale\": \"" + "en" + "\"}");

            httpPost.addHeader("Content-Type", "text/html"); //or text/plain
            httpPost.addHeader("Accept-Encoding", "gzip, deflate, sdch");
            httpPost.setEntity(params);
            HttpResponse response = client.execute(httpPost);
            int statuscode = response.getStatusLine().getStatusCode();
            String responseBody = EntityUtils.toString(response.getEntity());
            if (statuscode == 200) {
                System.out.println(responseBody);
            }
            if (statuscode != 200) {
                System.out.println(responseBody);
//                JSONObject obj = new JSONObject(responseBody);
//                JSONArray errors = obj.getJSONArray("errors");
//                String errorMessage = "";
//                if (errors.length() > 0) {
//                    errorMessage = errors.getJSONObject(0).getString("developerMessage");
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
        }

HttpGet is pretty much the same.

Upvotes: 1

Related Questions