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

huangapple go评论103阅读模式
英文:

Converting from DescribeSObjectResult to JsonArray (or to HttpEntity)

问题

几个星期前,我提出了一个关于如何使用SOAP API而不是REST API读取Salesforce数据的问题(参见https://stackoverflow.com/questions/63455670/trying-to-use-apache-gobblin-to-read-salesforce-data-using-soap-apis-instead-o ),但不幸的是,没有人回答,所以我正在尝试直接实现这个解决方案(在一点帮助下)。

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

// URL以Salesforce REST API终端点开头,并以"/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 });
        // 由于我们仅描述了一个sObject,所以在DescribeSObjectResult数组中应该只有一个元素。
        DescribeSObjectResult dsr = dsrArray[0];
        String jsonStr = ...;  /* 这是我需要帮助的地方,将dsr转换为类似的JSON字符串。 */
    } 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对象方面,任何形式的帮助都将不胜感激。

英文:

A couple of weeks ago, I asked a question about reading Salesforce data using the SOAP API instead of the REST API (see https://stackoverflow.com/questions/63455670/trying-to-use-apache-gobblin-to-read-salesforce-data-using-soap-apis-instead-o ), but unfortunately nobody answered it, and so I am trying to implement the solution (with a little help) directly.

Using the REST API, the existing code that reads in a table's definition (by making a call to the REST API) looks like this:

// 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;
}

I would like to write a method that uses the Salesforce SOAP API (using the generated "partner.wsdl" file) similar to the following incomplete code:

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;
}

Any sort of help in determining how to convert from the DescribeSObjectResult object to a similar JsonString / HttpEntity / StringEntity object would be greatly appreciated.

答案1

得分: 0

你已经消费了WSDL,对吗?DescribeSObjectResult 在你的项目中是一个普通的类。所以… 我的 Java 有点生疏,但问题似乎很简单,就是“如何将 Java 对象转换为 JSON”?

有一些库可以做到这一点,对吗?比如 Jackson。这个有帮助吗?https://stackoverflow.com/q/15786129/313628

我不确定最终结果是否完全相同,但应该足够接近。

英文:

You have consumed the WSDL, right? The DescribeSObjectResult is a normal class in your project. So... my Java is rusty but seems the question is simple "how to convert a Java object to JSON"?

There are libraries for this, right? Jackson for example. This helps? https://stackoverflow.com/q/15786129/313628

I'm not sure if you'll end with identical result but should be close enough.

huangapple
  • 本文由 发表于 2020年8月29日 07:11:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63641909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定