从 DescribeSObjectResult 转换为 JsonArray(或转换为 HttpEntity)

Converting from DescribeSObjectResult to JsonArray (or to HttpEntity)

几周前,我问了一个关于使用 SOAP API 而不是 REST API 读取 Salesforce 数据的问题(参见 Trying to use Apache Gobblin to read Salesforce data using SOAP API(s) instead of REST API ),但不幸的是没有人回答,所以我尝试直接实施解决方案(在一点帮助下)。

使用 REST API,读取 table 定义的现有代码(通过调用 REST API)如下所示:

// the URL starts with the Salesforce REST API endpoint, and ends with "/describe"
public String getSchema(String url, String accessToken, HttpClient httpClient) {
    String jsonStr;
    HttpRequestBase httpRequest = new HttpGet(url);
    if (accessToken != null) {
        httpRequest.addHeader("Authorization", "OAuth " + this.accessToken);
    }
    httpRequest.addHeader("Content-Type", "application/json");
    HttpEntity httpEntity = null;
    HttpResponse httpResponse = null;
    try {
        httpResponse = httpClient.execute(httpRequest);
        StatusLine status = httpResponse.getStatusLine();
        httpEntity = httpResponse.getEntity();

        if (httpEntity != null) {
            jsonStr = EntityUtils.toString(httpEntity);
        }

        if (status.getStatusCode() >= 400) {
            System.out.println("There was an error.  Http Status code of " + status.getStatusCode());
            EntityUtils.consumeEntity(httpEntity);
            return null;
        }
        return jsonStr;
    } catch (Exception e) {
       e.printStackTrace();
       return null;
    }
    return jsonStr;
}

我想编写一个使用 Salesforce SOAP API(使用生成的“partner.wsdl”文件)的方法,类似于以下不完整的代码:

public String getSchemaViaSoap(String tableName) {
    String jsonStr;
    PartnerConnection partnerConnection = ...;
    try {
        DescribeSObjectResult[] dsrArray = partnerConnection.describeSObjects(new String[] { entity });
        // Since we described only one sObject, we should have only
        // one element in the DescribeSObjectResult array.
        DescribeSObjectResult dsr = dsrArray[0];
        String jsonStr = ...;  /* this is where I need help in converting dsr into a similar JSON string. */
    } catch (ConnectionException e) {
        e.printStackTrace();
        log.error("Error getting connection", e);
        System.out.println("Error getting connection" + e);
        return null;
    }
    return jsonStr;
}

对于确定如何从 DescribeSObjectResult 对象转换为类似的 JsonString/HttpEntity/StringEntity 对象的任何帮助,我们将不胜感激。

你消费过WSDL对吧? DescribeSObjectResult 在您的项目中是一个普通的 class。所以...我的 Java 生锈了,但问题似乎很简单“如何将 Java 对象转换为 JSON”?

有这方面的图书馆,对吧​​? Jackson for example. This helps? Converting Java objects to JSON with Jackson

我不确定你是否会得到相同的结果,但应该足够接近了。