1

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

使用 REST API,读取表定义的现有代码(通过调用 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 对象将不胜感激。

4

1 回答 1

0

您已经使用了 WSDL,对吗?这DescribeSObjectResult是您项目中的普通类。所以...我的 Java 生锈了,但问题似乎很简单“如何将 Java 对象转换为 JSON”?

有这方面的图书馆,对吧​​?以杰克逊为例。这有帮助吗?使用 Jackson 将 Java 对象转换为 JSON

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

于 2020-08-29T00:05:47.937 回答