【问题标题】:jackson.databind.ObjectMapper upperCasing json Array and Object namesjackson.databind.ObjectMapper upperCasing json 数组和对象名称
【发布时间】:2020-02-05 00:26:29
【问题描述】:

我正在尝试使用 JMSTemplate 将 JSON 消息发布到主题。这段代码已经存在于一个应用程序中,我只是将它复制到另一个应用程序中,因为我们正试图将两个应用程序合并为一个。我发现代码现在正在发送 JSON 消息,其中 JSONArrayJSONObject 字段名称的首字母大写。

我正在使用带有消息转换器的 JMS 模板,该消息转换器接收对象映射器以将 POJO 转换为 JSON。我的新代码唯一真正的区别是我使用的是更新版本的 spring boot。我知道这会更新所有杰克逊依赖项,所以也许这就是发生这种变化的原因。我最终尝试在我的对象映射器上设置命名策略,但这似乎不起作用。我最初是在我的 bean 定义中做的,但为了看看它是否真的有效,我在做 convertAndSend 之前尝试了它,但它没有用。我仍然得到大写的 JSON 对象和数组名称。

public void sendMessage(Object responseToSend) {

    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);// does not seem to make a difference
    try {
        System.out.println(objectMapper.writeValueAsString(responseToSend));//prints array and object names with the first letter capitolized
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    jmsTemplatePublish.convertAndSend("REDACTED",
            responseToSend);
}

所以,例如,我的新应用程序正在发送类似的东西。

"Quote":[{"QuoteInformation":{"Inputs":{"exampleField":false,"ExampleWritten":{"dwelling":true}}

以前是这样的

"quote":[{"quoteInformation":{"inputs":{"exampleField":false,"exampleWritten":{"dwelling":true}}

【问题讨论】:

    标签: java json spring-boot jackson-databind jmstemplate


    【解决方案1】:

    @Kachopsticks 您是否尝试过在 objectMapper 命名策略配置中使用 PropertyNamingStrategy.LOWER_CASE 而不是使用 PropertyNamingStrategy.LOWER_CAMEL_CASE .

    【讨论】:

    • 如此有趣的行为。我按照您的建议进行了更改,我看到所有字符串字段名称都更改为全部小写,但我的 JSONArray 和 JSONObjects 的名称仍然是大写骆驼。那么 PropertyNamingStrategy 是否只适用于字段而不适用于数组或对象?
    【解决方案2】:

    这颗豆子是罪魁祸首。必须删除 .modulesToInstall(JaxbAnnotationModule.class);

    @SuppressWarnings("unchecked")
    @Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
        return Jackson2ObjectMapperBuilder.json()
                .serializationInclusion(JsonInclude.Include.NON_EMPTY)
                .defaultViewInclusion(true)
                .modulesToInstall(JaxbAnnotationModule.class);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多