【问题标题】:Spring boot jackson non_null property not workingSpring Boot jackson non_null 属性不起作用
【发布时间】:2018-12-18 02:20:34
【问题描述】:

我正在使用 Spring boot 1.5.7-RELEASE

我正在尝试在我的应用程序中设置 global non null Jackson 属性。

但它不起作用。

我在 application.properties 和 bootstrap.properties 中都尝试过,但都没有工作。

spring.jackson.default-property-inclusion=NON_NULL
spring.jackson.serialization-inclusion=NON_NULL

但是当我申请班级级别时,它工作正常。

@JsonInclude(JsonInclude.NON_NULL)

【问题讨论】:

标签: spring-boot jackson


【解决方案1】:

根据documentation,正确答案是:

spring.jackson.default-property-inclusion=non_null

(注意小写的 non_null - 这可能是您的问题的原因)

编辑: 我创建了一个简单的 Spring Boot 1.5.7.RELEASE 项目,只有以下两个编译依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency> 

然后我添加了以下控制器和响应类(使用 Lombok 跳过一些样板代码):

@RestController
@RequestMapping("/jackson")
public class JacksonTestController {

    @GetMapping("/test")
    public Response test() {
        val response = new Response();
        response.setField1("");

        return response;
    }
}

@Data
class Response {
    private String field1;
    private String field2;
    private Integer field3;
}

最后我按照文档配置了 Jackson,运行应用程序并导航到 http://localhost:8080/jackson/test。结果是(如预期的那样):

{"field1":""}

之后,我深入研究了 Spring Boot 的源代码,发现 Spring 使用类 org.springframework.http.converter.json.Jackson2ObjectMapperBuilder 来创建 com.fasterxml.jackson.databind.ObjectMapper 的实例。然后,我在上述构建器类的方法public &lt;T extends ObjectMapper&gt; T build() 中放置一个断点,并在调试模式下运行我的应用程序。

我发现在应用程序启动期间创建了 8 个ObjectMapper 实例,其中只有一个是使用application.properties 文件的内容配置的。 OP 从未具体说明他是如何使用序列化的,因此他的代码可能引用了其他 7 个可用的对象映射器之一。

无论如何,确保应用程序中的所有对象映射器都配置为仅序列化非空属性的唯一方法是创建自己的 org.springframework.http.converter.json.Jackson2ObjectMapperBuilder 类副本并将该选项硬编码为默认值或自定义在每次调用它的构造函数或构建方法时读取application.properties 的类。

【讨论】:

  • 我的版本似乎更喜欢 NON_NULL
  • 嘿,你怎么知道有 8 个 ObjectMapper 实例?有没有办法使用 Intellij 或 Eclipse 在运行时调试某个对象的实例数量?谢谢
  • 我在回复中写道:“之后我深入研究了 Spring Boot 的源代码,发现 Spring 使用类 org.springframework.http.converter.json.Jackson2ObjectMapperBuilder 来创建 com.fasterxml 的实例。 jackson.databind.ObjectMapper。然后我在上述构建器类的方法 public T build() 中放置一个断点,并在调试模式下运行我的应用程序。"
【解决方案2】:

也许我迟到了,但它可能对某人有帮助。

扩展 WebMvcConfigurationSupport 类,按照自己的方式自定义 Springboot 配置。

@Configuration
public class Config extends WebMvcConfigurationSupport{

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        converter.setObjectMapper(mapper);
        converters.add(converter);
        super.configureMessageConverters(converters);
    }
}

【讨论】:

    【解决方案3】:

    我只是在处理application.properties 中的设置,两者都没有。就我而言,我正在扩展一个抽象配置类,它定义了一个具有完全不同设置的ObjectMapper bean。所以我不得不重写它。

    是什么让我找到了使用 Spring Boot 应用程序具有的 /beans Actuator 端点并搜索“ObjectMapper”的地方。它揭示了一个我没有意识到正在创建的实例。

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 2016-08-06
      • 1970-01-01
      • 2018-05-30
      • 2019-05-30
      • 2018-08-28
      • 1970-01-01
      • 2023-02-14
      • 2019-08-12
      相关资源
      最近更新 更多