简体   繁体   English

用于创建对象的Salesforce Java Rest API JSON结构

[英]Salesforce Java rest api json structure for object creation

I am trying to set up REST API integration for custom java application with Salesforce. 我正在尝试使用Salesforce为自定义Java应用程序设置REST API集成。 I have followed the tutorials/resources namely- 我遵循了教程/资源-

https://developer.salesforce.com/blogs/developer-relations/2017/04/machine-machine-salesforce-integrations-java-rest-soap.html https://developer.salesforce.com/blogs/developer-relations/2017/04/machine-machine-salesforce-integrations-java-rest-soap.html

http://www.asagarwal.com/step-by-step-guide-to-get-started-with-salesforce-rest-api-using-java/ http://www.asagarwal.com/step-by-step-guide-to-get-started-with-salesforce-rest-api-using-java/

These work perfectly fine. 这些工作正常。 My question deals with creating/updating Salesforce object via REST api. 我的问题涉及通过REST API创建/更新Salesforce对象。 The particular code snippet to construct the respective object to create is-| 构造要创建的各个对象的特定代码段是-|

        //create the JSON object containing the new lead details.
        JSONObject lead = new JSONObject();
        lead.put("FirstName", "REST API");
        lead.put("LastName", "Lead");
        lead.put("Company", "asagarwal.com");

And then there is the part to construct this http request which is fairly usual- 然后是构建此http请求的部分,这很常见-

          //Construct the objects needed for the request
        HttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost httpPost = new HttpPost(uri);
        httpPost.addHeader(oauthHeader);
        httpPost.addHeader(prettyPrintHeader);
        // The message we are going to post
        StringEntity body = new StringEntity(lead.toString(1));
        body.setContentType("application/json");
        httpPost.setEntity(body);  

Constructing a json object in this fashion seems to me like it would be a maintenance issue. 在我看来,以这种方式构造json对象似乎是一个维护问题。 This involves right now having to go through the entire fields list of an object which could be 100s, and getting their field name. 现在,这涉及到必须遍历对象的整个字段列表(可能是100s)并获取其字段名称。 This seems really tedious. 这似乎很乏味。 Is there a way where we can get the object structure of the particular Salesforce object in java and then perhaps I can use external libraries like Jackson and its annotations to serialize the object into json instead of manually having to construct this json object? 有没有一种方法可以在Java中获取特定Salesforce对象的对象结构,然后也许我可以使用外部库(例如Jackson和其批注)将对象序列化为json,而不必手动构造此json对象? I have tried to look for this online but the only way currently possible is to actually get the field names and construct a java bean myself and have that serialized to json. 我尝试过在线查找此方法,但是当前唯一可行的方法是实际获取字段名称并自己构造一个Java bean并将其序列化为json。 Any cleaner way to do this perhaps using any of Salesforce' capabilities? 有没有更干净的方法可以使用Salesforce的任何功能来做到这一点?

Do you have the flexibility to setup a REST service in an Apex class that you could pass an object type to as a String? 您是否具有在Apex类中设置REST服务(可以将对象类型作为String传递)的灵活性?

You could write a web service that returns the JSON of an sObject by using JSON.serialize() on the SF side and returning it based on what was passed into the service. 您可以编写一个Web服务来返回sObject的JSON,方法是在SF端使用JSON.serialize()并根据传递给该服务的内容返回它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM