Home > Software design >  Case Sensitive POJO to Json mapping Hybris
Case Sensitive POJO to Json mapping Hybris

Time:04-21

How can I keep the original case of the keys when writing the object to json?

POJO-Class:

public class LeadRequest
{
    private String AccountName;
    private String AccountAlias;
    private String BPID;
    private String CustomerType;
    private String Email;
    private String LocationType;
    private String APRID;
    private String APRDistributorName;
    private String EngagedwithRAOrDistributor;

    public String getBPID()
    {
        return BPID;
    }
    public void setBPID(final String bPID)
    {
        BPID = bPID;
    }
    public String getEngagedwithRAOrDistributor()
    {
        return EngagedwithRAOrDistributor;
    }
    public void setEngagedwithRAOrDistributor(final String engagedwithRAOrDistributor)
    {
        EngagedwithRAOrDistributor = engagedwithRAOrDistributor;
    }
}

Service-class:

public void submitLeadRequest(final LeadRequest lead)
{
    try
    {
        final String endPoint = Config.getParameter(ServicesConstants.API_URL);
        final HttpPost request = new HttpPost(endPoint);
        request.addHeader(ServicesConstants.CONTENT_TYPE, ServicesConstants.APPLICATION_JSON);
        request.addHeader(ServicesConstants.CLIENT_ID, Config.getParameter(ServicesConstants.CLIENT_ID));
        request.addHeader(ServicesConstants.CLIENT_SECRET, Config.getParameter(ServicesConstants.CLIENT_SECRET));

        final ObjectMapper mapper = new ObjectMapper();
        final String jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(lead);
        final StringEntity entity = new StringEntity(jsonString);
        request.setEntity(entity);
        final RequestConfig requestConfig = getRequestConfig(API_TIMEOUT_LONG);
        final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
        CloseableHttpResponse response = client.execute(request);      
    }
}

Currently the Post Request json generated is:

{
  "accountAlias" : "No Account Alias",
  "accountName" : "REI AUTOMATION INC",
  "customerType" : "OEM",
  "aprid" : "002",
  "bpid" : "0099105850",
  "locationType" : "Research & Development",
  "email" : "[email protected]",
  "engagedwithRAOrDistributor" : "",
  "aprdistributorName" : "002-CED Royal Industrial Elec"
}

But the post request is failing giving HTTP/1.1 500 Server Error because of case sensitive keys in request json for the system being called

Therefore, the desired Request Json is: enter image description here

CodePudding user response:

If you are using com.fasterxml.jackson.databind.ObjectMapper, you can specify final name for each field using com.fasterxml.jackson.annotation.JsonProperty for example:

@JsonProperty("AccountName")
private String AccountName;

Or you can “tell” to your mapper to use fields instead of getters for creating a final JSON. In order to do so you can just configure your mapper class as follows:

mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
  • Related