区分大小写的 POJO 到 Json 映射 Hybris

Case Sensitive POJO to Json mapping Hybris

如何在将对象写入 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;
    }
}

服务-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);      
    }
}

当前生成的 Post 请求 json 是:

{
  "accountAlias" : "No Account Alias",
  "accountName" : "REI AUTOMATION INC",
  "customerType" : "OEM",
  "aprid" : "002",
  "bpid" : "0099105850",
  "locationType" : "Research & Development",
  "email" : "john.smith@jefftestaccount.com",
  "engagedwithRAOrDistributor" : "",
  "aprdistributorName" : "002-CED Royal Industrial Elec"
}

但是 post 请求失败,给出 HTTP/1.1 500 服务器错误,因为被调用系统 json 的请求中的键区分大小写

因此,所需的请求 Json 是:

如果您使用 com.fasterxml.jackson.databind.ObjectMapper,您可以使用 com.fasterxml.jackson.annotation.JsonProperty 为每个字段指定最终名称,例如:

@JsonProperty("AccountName")
private String AccountName;

或者您可以“告诉”您的映射器使用字段而不是 getter 来创建最终 JSON。为此,您只需按如下方式配置映射器 class:

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