Groovy 打印Apache HttpClient PostMethod调用的有效负载

Groovy 打印Apache HttpClient PostMethod调用的有效负载,groovy,apache-commons-httpclient,Groovy,Apache Commons Httpclient,我使用ApacheHttpClient 3.1向url发出HTTP Post请求,在这里我传递一个JSON请求实体和一些参数,如下所示 HttpMethod method = new PostMethod("https://starfleet.reliant/commandConsole") // assume there is already a varaible json which has valid JSON in it StringRequestEntity re

我使用ApacheHttpClient 3.1向url发出HTTP Post请求,在这里我传递一个JSON请求实体和一些参数,如下所示

    HttpMethod method = new PostMethod("https://starfleet.reliant/commandConsole")
   // assume there is already a varaible json which has valid JSON in it
    StringRequestEntity requestEntity = new StringRequestEntity(json.toString(), "application/json", "UTF-8")
    method.setRequestEntity(requestEntity);
    method.addParameter("format","json");
    method.addParameter("prefixCode","16309");
有没有一种方法可以打印出整个负载的样子(包括JSON请求实体和参数)?换句话说,我想确切地看到发布到服务器上的内容

如果我这么做

println("Sending this ${method.toString()}")
这只是打印

  Sending this org.apache.commons.httpclient.methods.PostMethod@3f67dcc1 
这当然不是我想要的。

对于HttpClient 3x:

请尝试对requestentity使用如下对象映射器:

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(requestEntity));
依赖关系:

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.3</version>
</dependency>

扩展PostMethod并添加您自己的toString()@Joseph,一定有更简单的方法!谢谢你的建议,但我认为这只适用于ApacheHttpClient 4.0+,但我一直受3的困扰,升级起来很痛苦。正确..虽然值得升级..更新了答案..它将以json等字符串格式打印请求实体..例如:
{“content”:“{”a\:“b\”,“charset”:“UTF-8”,“contentType”:“application/json;charset=UTF-8”,“contentLength”:9,“repeatable”:true}
String str = EntityUtils.toString(method.getRequestEntity());
System.out.println(str);