簡體   English   中英

區分大小寫的 POJO 到 Json 映射 Hybris

[英]Case Sensitive POJO to Json mapping Hybris

將object寫入json時,如何保持按鍵的原殼?

POJO 類:

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

服務等級:

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 Request 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"
}

但是由於正在調用的系統的請求 json 中的區分大小寫的鍵,發布請求未能給出 HTTP/1.1 500 服務器錯誤

因此,所需的 Request 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM