案例敏感的pojo对json映射杂种

发布于 2025-01-22 16:28:50 字数 2669 浏览 0 评论 0原文

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

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

当前生成的邮政请求是:

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

但是,邮政请求未能给予HTTP/1.1 500服务器错误,因为在请求JSON中的案例敏感键json中的系统被称为系统

,因此所需的请求json是:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2025-01-29 16:28:51

如果您使用com.fasterxml.jackson.databind.objectMapper,则可以使用com.fasterxml.jackson.jackson.jsonproperty例如:

@JsonProperty("AccountName")
private String AccountName;

或您可以“告诉”映射器使用字段而不是制作最终JSON的getters。为了这样做,您可以按以下方式配置映射器类:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文