How to post JSONObject to RESTful service and read response using java
<p>I am trying to call POST operation of the service with JSON request body and read JSON response.</p>

<p>I have below method to build the JSON request as JSONObject object.</p>

<pre><code>public class CashFundTransfer {
public JSONObject buildRequest(){
    JSONObject request = new JSONObject();
    request.put("dateTime", getDateTimeObj());
    request.put("businessDate", getBusinessDateObj());
    request.put("description", getDescriptionObj());
    request.put("transferAccount", buildTransferAccount());
    request.put("amount", buildAmount());
    request.put("businessUnit", buildBusinessUnit());
    request.put("status", buildStatus());
    request.put("recipient", buildRecipient());
    request.put("cashFundTransferee", buildCashFundTransferee());
    request.put("enteringAssoc", buildEnteringAssoc());
    request.put("approvingAssoc", buildApprovingAssoc());
    request.put("accountingDivision", buildAccountingDivision());
    return request;
   }
}
</code></pre>

<p>All methods that that I call in put return a JSONObject's object that is a child node of the parent.</p>

<p>In my test class, I have the below code. When I execute the code, service is recieving hashcode of the StringEntity object, instead of the content.</p>

<pre><code>public class CFTTest {
   CashFundTransfer cft = new CashFundTransfer();
   JSONObject request = cft.buildRequest();
   System.out.println("request: "+request);

   public void callService() throws IOException{
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(url);
        try {
          String inputString = request.toString();
          System.out.println("inputString 1: "+inputString);
          StringEntity input = new StringEntity(inputString);
          System.out.println("input: "+input);
          } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
          }
          input.setContentType("application/json");
          postRequest.setEntity(input);
          try {
               response = httpClient.execute(postRequest);
          } catch (ClientProtocolException e) {
                e.printStackTrace();
          } catch (IOException e) {
                e.printStackTrace();
          }
          System.out.println("response: "+response);
      }
}
</code></pre>

<p>Below is the console when I execute this code.</p>

<pre><code>request: {"dateTime":"2016-12-04","businessDate":"2016-12-04","amount":      {"Value":200.0,"currency":{"code":"USD","description":"dollar"}},"businessUnit":{"number":5525,"name":"WMT","location":{"countryCode":"US"},"id":"0"},"recipient":{"name":{"fullName":"Samuel"}},"description":"Store Use 1","transferAccount":{"id":992},"accountingDivision":{"number":"1"},"enteringAssoc":{"id":"0"},"cashFundTransferee":{"id":"0","type":"OPERATOR"},"approvingAssoc":{"id":"0"},"status":{"code":"1"}}
inputString 1: {"dateTime":"2016-12-04","businessDate":"2016-12-04","amount":{"Value":200.0,"currency":{"code":"USD","description":"dollar"}},"businessUnit":{"number":5525,"name":"WMT","location":{"countryCode":"US"},"id":"0"},"recipient":{"name":{"fullName":"Samuel"}},"description":"Store Use 1","transferAccount":{"id":992},"accountingDivision":{"number":"1"},"enteringAssoc":{"id":"0"},"cashFundTransferee":{"id":"0","type":"OPERATOR"},"approvingAssoc":{"id":"0"},"status":{"code":"1"}}
input: org.apache.http.entity.StringEntity@737996a0
14:06:22.238 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Get    connection for route {s}-   &gt;https://nodejs.dev.cashfundtransfer.bofbua.prod.cloud.wal-mart.com
14:06:22.506 [main] DEBUG o.a.h.i.c.DefaultClientConnectionOperator - Connecting to nodejs.dev.cashfundtransfer.bofbua.prod.cloud.wal-mart.com:443
14:06:22.609 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: best-match
14:06:22.630 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache   not set in the context
14:06:22.630 [main] DEBUG o.a.h.c.p.RequestTargetAuthentication - Target auth state: UNCHALLENGED
14:06:22.631 [main] DEBUG o.a.h.c.p.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
14:06:22.631 [main] DEBUG o.a.h.impl.client.DefaultHttpClient - Attempt 1 to execute request
14:06:22.632 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Sending request: POST /api/cashfundtransfer/v1 HTTP/1.1
14:06:22.632 [main] DEBUG org.apache.http.wire -  &gt;&gt; "POST /api/cashfundtransfer/v1 HTTP/1.1[\r][\n]"
14:06:22.634 [main] DEBUG org.apache.http.wire -  &gt;&gt; "Content-Length: 475[\r][\n]"
14:06:22.634 [main] DEBUG org.apache.http.wire -  &gt;&gt; "Content-Type: text/plain; charset=ISO-8859-1[\r][\n]"
14:06:22.634 [main] DEBUG org.apache.http.wire -  &gt;&gt; "Host: nodejs.dev.cashfundtransfer.bofbua.prod.cloud.wal-mart.com[\r][\n]"
14:06:22.634 [main] DEBUG org.apache.http.wire -  &gt;&gt; "Connection: Keep-Alive[\r][\n]"
14:06:22.635 [main] DEBUG org.apache.http.wire -  &gt;&gt; "User-Agent: Apache-HttpClient/4.3.5 (java 1.5)[\r][\n]"
14:06:22.635 [main] DEBUG org.apache.http.wire -  &gt;&gt; "[\r][\n]"
14:06:22.635 [main] DEBUG org.apache.http.headers - &gt;&gt; POST /api/cashfundtransfer/v1 HTTP/1.1
14:06:22.635 [main] DEBUG org.apache.http.headers - &gt;&gt; Content-Length: 475
14:06:22.635 [main] DEBUG org.apache.http.headers - &gt;&gt; Content-Type: text/plain; charset=ISO-8859-1
14:06:22.635 [main] DEBUG org.apache.http.headers - &gt;&gt; Host: nodejs.dev.cashfundtransfer.bofbua.prod.cloud.wal-mart.com
14:06:22.635 [main] DEBUG org.apache.http.headers - &gt;&gt; Connection: Keep-Alive
14:06:22.635 [main] DEBUG org.apache.http.headers - &gt;&gt; User-Agent: Apache-HttpClient/4.3.5 (java 1.5)
14:06:22.636 [main] DEBUG org.apache.http.wire -  &gt;&gt; "{"dateTime":"2016-12-04","businessDate":"2016-12-04","amount":{"Value":200.0,"currency":{"code":"USD","description":"dollar"}},"businessUnit":{"number":5525,"name":"WMT","location":{"countryCode":"US"},"id":"0"},"recipient":{"name":{"fullName":"Samuel"}},"description":"Store Use 1","transferAccount":{"id":992},"accountingDivision":{"number":"1"},"enteringAssoc":{"id":"0"},"cashFundTransferee":{"id":"0","type":"OPERATOR"},"approvingAssoc":{"id":"0"},"status":{"code":"1"}}"
14:06:22.653 [main] DEBUG org.apache.http.wire -  &lt;&lt; "HTTP/1.1 400 Bad Request[\r][\n]"
14:06:22.658 [main] DEBUG org.apache.http.wire -  &lt;&lt; "content-type: application/json; charset=utf-8[\r][\n]"
14:06:22.659 [main] DEBUG org.apache.http.wire -  &lt;&lt; "strict-transport-security: max-age=15768000[\r][\n]"
14:06:22.659 [main] DEBUG org.apache.http.wire -  &lt;&lt; "x-frame-options: DENY[\r][\n]"
14:06:22.659 [main] DEBUG org.apache.http.wire -  &lt;&lt; "x-xss-protection: 1; mode=block[\r][\n]"
14:06:22.659 [main] DEBUG org.apache.http.wire -  &lt;&lt; "x-download-options: noopen[\r][\n]"
14:06:22.659 [main] DEBUG org.apache.http.wire -  &lt;&lt; "x-content-type-options: nosniff[\r][\n]"
14:06:22.659 [main] DEBUG org.apache.http.wire -  &lt;&lt; "cache-control: no-cache[\r][\n]"
14:06:22.660 [main] DEBUG org.apache.http.wire -  &lt;&lt; "content-length: 197[\r][\n]"
14:06:22.660 [main] DEBUG org.apache.http.wire -  &lt;&lt; "Date: Wed, 21 Dec 2016 20:06:23 GMT[\r][\n]"
14:06:22.660 [main] DEBUG org.apache.http.wire -  &lt;&lt; "Connection: keep-alive[\r][\n]"
14:06:22.660 [main] DEBUG org.apache.http.wire -  &lt;&lt; "[\r][\n]"
14:06:22.661 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Receiving response: HTTP/1.1 400 Bad Request
14:06:22.661 [main] DEBUG org.apache.http.headers - &lt;&lt; HTTP/1.1 400 Bad Request
14:06:22.661 [main] DEBUG org.apache.http.headers - &lt;&lt; content-type: application/json; charset=utf-8
14:06:22.661 [main] DEBUG org.apache.http.headers - &lt;&lt; strict-transport-security: max-age=15768000
14:06:22.661 [main] DEBUG org.apache.http.headers - &lt;&lt; x-frame-options: DENY
14:06:22.661 [main] DEBUG org.apache.http.headers - &lt;&lt; x-xss-protection: 1; mode=block
14:06:22.661 [main] DEBUG org.apache.http.headers - &lt;&lt; x-download-options: noopen
14:06:22.661 [main] DEBUG org.apache.http.headers - &lt;&lt; x-content-type-options: nosniff
14:06:22.661 [main] DEBUG org.apache.http.headers - &lt;&lt; cache-control: no-cache
14:06:22.661 [main] DEBUG org.apache.http.headers - &lt;&lt; content-length: 197
14:06:22.662 [main] DEBUG org.apache.http.headers - &lt;&lt; Date: Wed, 21 Dec 2016 20:06:23 GMT
14:06:22.662 [main] DEBUG org.apache.http.headers - &lt;&lt; Connection: keep-alive
14:06:22.667 [main] DEBUG o.a.h.impl.client.DefaultHttpClient - Connection can be kept alive indefinitely
response: HTTP/1.1 400 Bad Request [content-type: application/json; charset=utf-8, strict-transport-security: max-age=15768000, x-frame-options: DENY, x-xss-protection: 1; mode=block, x-download-options: noopen, x-content-type-options: nosniff, cache-control: no-cache, content-length: 197, Date: Wed, 21 Dec 2016 20:06:23 GMT, Connection: keep-alive]
</code></pre>

<p>Unable to identify, where I am going wrong on this.</p>