在Spring Boot应用程序中配置Jackson Mixin

时间:2018-07-05 13:40:11

标签: java spring-boot jackson mixins jackson2

我为我的班级创建了一个mixin。 mixin本身可以很好地工作,这不是大多数人在哪里混合faterxml / codehaus注释的问题。 我在单元测试中对其进行了测试,并使用addMixIn方法“手动”创建了ObjectMapper-它工作得很好。

我想使用该mixin修改从我的REST端点返回的响应json。 我试图以多种不同方式自定义Spring Boot的ObjectMapper:

BuilderCustomizer:

@Bean
public Jackson2ObjectMapperBuilderCustomizer addMixin(){
    return new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
            jacksonObjectMapperBuilder.mixIn(MyClass.class, MyClassMixin.class);                
        }
    };
}

Builder:

@Bean
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
    return new Jackson2ObjectMapperBuilder().mixIn(MyClass.class, MyClassMixin.class);
}

转换器:

@Bean
public MappingJackson2HttpMessageConverter configureJackson(){
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    ObjectMapper mapper = new ObjectMapper();
    mapper.addMixIn(MyClass.class, MyClassMixin.class);
    converter.setObjectMapper(mapper);
    return converter;
}

ObjectMapper:

@Autowired(required = true)
public void configureJackon(ObjectMapper jsonMapper){
    jsonMapper.addMixIn(MyClass.class, MyClassMixin.class);
}

这些工作都没有。

2 个答案:

答案 0 :(得分:1)

这可能取决于Spring Boot的版本,但是根据76.3 Customize the Jackson ObjectMapper定义一个新的Jackson2ObjectMapperBuilderCustomizer bean就足够了

  

可以通过一个或多个Jackson2ObjectMapperBuilderCustomizer bean来定制上下文的Jackson2ObjectMapperBuilder。可以对此类定制器bean进行排序(Boot自己的定制器的顺序为0),从而可以在Boot定制之前和之后应用其他定制。

答案 1 :(得分:0)

我尝试了以上方法,但对我也不起作用。调试时,我注意到消息转换器内的ObjectMapper为空。

参考帖子get registered message converters,我最终替换了Jackson的默认消息转换器,从而使我可以根据自己的需要自定义对象映射器:

@SpringBootApplication
@RestController
public class MixinTest {

    public static void main(String[] args) {
        SpringApplication.run(MixinTest.class, args);
    }

    static class Person {
        private String title;
        private String name;
        private String nullField;
        private LocalDate date;

        Person(String title, String name) {
            this.title = title;
            this.name = name;
            this.date = LocalDate.now();
        }
        // getters here...
    }

    // this will exclude nullField 
    @JsonInclude(JsonInclude.Include.NON_NULL)
    interface PersonMixin {
        @JsonProperty("fullName")
        String getName();
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer personCustomizer() {
        return jacksonObjectMapperBuilder ->
            jacksonObjectMapperBuilder.mixIn(Person.class, PersonMixin.class);
    }

    @Bean
    public MappingJackson2HttpMessageConverter myMessageConverter(
            // provided by Spring
            RequestMappingHandlerAdapter reqAdapter,
            Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
        ObjectMapper mapper = jacksonObjectMapperBuilder
            .featuresToEnable(SerializationFeature.INDENT_OUTPUT)
            .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .modulesToInstall(new JavaTimeModule())
            .build();

        // **replace previous MappingJackson converter**
        List<HttpMessageConverter<?>> converters = 
            reqAdapter.getMessageConverters();
        converters.removeIf(httpMessageConverter ->
            httpMessageConverter.getClass()
                .equals(MappingJackson2HttpMessageConverter.class));

        MappingJackson2HttpMessageConverter jackson = new 
            MappingJackson2HttpMessageConverter(mapper);
        converters.add(jackson);
        reqAdapter.setMessageConverters(converters);
        return jackson;
    }

    @GetMapping("/test")
    public Person get() {
        return new Person("Mr", "Joe Bloggs");
    }
}

在点击http://localhost:8080/test后在浏览器中输出以下内容:

{
    "title" : "Mr",
    "date" : "2019-09-03",
    "fullName" : "Joe Bloggs"
}

这样,我应该能够根据需要添加尽可能多的定制器。我敢肯定有更好的方法可以做到这一点。替换这样的内部部件似乎很困难...