简体   繁体   中英

How to make JSON request body configurable in Java

I am writing a rest request from Java app to salesforce. My end point may change their fields from time to time. So I want to make request body field names configuration either through xml or properties file, so that if body is added in end point side, the same we will add in our property and work will be done with minimum changes. Can any one suggest me if I will create a property file like below and how can we set this value ${recodeTypeID} from core Java application?

config.properties
RecordTypeId=${recodeTypeID}
FirstName=${FirstName}

or otherwise in xml in which display tag having field name and input having value.

  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <leads>
  <fields>
        <field>
            <display>"ID DATA SOURCE"</display>
            <input>ID_DATA_SOURCE</input>
        </field>
        <field>
            <display>"CPF"</display>
            <input>CPF</input>
        </field>
        <field>
            <display>"NM PERSON"</display>
            <input>NM_PERSON</input>
        </field>
        <field>
            <display>"TP GENDER"</display>
            <input>TP_GENDER</input>
        </field>
        <field>
            <display>"DT BIRTH DATE"</display>
            <input>DT_BIRTH_DATE</input>
        </field>
</leads>

My RequestBody is below:

JSONObject leads = new JSONObject();
         leads.put("RecordTypeId","0126A000000BAIzQAO");
         leads.put("FirstName", "Prabhat");
         leads.put("Company", "Tests Enterprises");
         leads.put("LeadProfile__c", "");
         leads.put("Email", "test@email.com");
         leads.put("Phone", "1155550000");
         leads.put("MobilePhone", "11955550000");
         leads.put("ContactPreference__c", "Email");
         leads.put("CNPJ__c", "10212469000148");
         leads.put("CPF__c", "");
         leads.put("Street", "");
         leads.put("PostalCode", "");
         leads.put("City", "");
         leads.put("State", "");
         leads.put("Country", "");
         leads.put("DealerCode__c", "04659");
         leads.put("TMA__c", "KHC");
         leads.put("Catalog__c", "KAA8");
         leads.put("ModelYear__c", "2018");
         leads.put("Color__c", "");
         leads.put("InternalFinish__c", "");
         leads.put("LeadSource", "Web");
         leads.put("LeadSubSource__c", "Website");
         leads.put("InterestType__c", "");
         leads.put("AgreeReceiveContact__c", "true");

Thanks.

The property file values would generally be hardcoded like: RecordTypeId=0126A000000BAIzQAO if that is what you are looking for.

Loading them into java with @Value would only work if we know the different data coming in before hand.

For your use case, I would recommend a comma separated values in the property file, something like

 config.properties
JSONElements=RecordTypeId, FirstName, Company, LeadProfile__c, Email ....

Then in the JAVA code

@value("JSONElements")
String jsonElements;


String [] elements = jsonElements.split(",");
List<String> values; // This would have values like 0126A000000BAIzQAO, Prabhat, Tests Enterprises
int itr;
for(itr = 0;itr < elements.length: itr++) {
    JSONObject leads = new JSONObject();
    leads.put(elements[itr], values[itr]);
}

If possible create a new Class for your record object and use a getRequestBody() method in your Class. Instead of maintaining a separate XML or properties file, you can just change the getRequestBody method in case of change in API request body.

Ex :

public class Record
{
   private String name;
   private String email;
   private String phone;
   private String country;

   public JSONObject getRequestBody()
   {
       JSONObject requestBody = new JSONObject();
       requestBody.put("FirstName", name);
       requestBody.put("EmailId", email);
       requestBody.put("PhoneNumber", phone);
       requestBody.put("CountryCode", country);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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