[英]Case Sensitive POJO to Json mapping Hybris
How can I keep the original case of the keys when writing the object to json?将object写入json时,如何保持按键的原壳?
POJO-Class: 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;
}
}
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:目前生成的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"
}
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但是由于正在调用的系统的请求 json 中的区分大小写的键,发布请求未能给出 HTTP/1.1 500 服务器错误
Therefore, the desired Request Json is:因此,所需的 Request Json 是:
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:如果您使用com.fasterxml.jackson.databind.ObjectMapper
,您可以使用com.fasterxml.jackson.annotation.JsonProperty
为每个字段指定最终名称,例如:
@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:或者您可以“告诉”您的映射器使用字段而不是 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.