在spring-boot-webflux中不使用已配置的ObjectMapper

我使用常规的Spring

Web控制器在我的objectmapperbuilder配置中配置了mixins,并根据mixins输出数据。但是,使用webflux时,具有返回Flow或Mono的方法的控制器会将数据序列化,就像对象映射器是默认值一样。

如何使webflux强制使用要使用的objectmapper配置?

样本配置:

@Bean

JavaTimeModule javatimeModule(){

return new JavaTimeModule();

}

@Bean

Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){

return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.featuresToEnable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)

.mixIn(MyClass.class, MyClassMixin.class);

}

回答:

实际上,我通过逐步执行init代码找到了解决方案:

@Configuration

public class Config {

@Bean

JavaTimeModule javatimeModule(){

return new JavaTimeModule();

}

@Bean

Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){

return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.featuresToEnable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)

.mixIn(MyClass.class, MyClassMixin.class);

}

@Bean

Jackson2JsonEncoder jackson2JsonEncoder(ObjectMapper mapper){

return new Jackson2JsonEncoder(mapper);

}

@Bean

Jackson2JsonDecoder jackson2JsonDecoder(ObjectMapper mapper){

return new Jackson2JsonDecoder(mapper);

}

@Bean

WebFluxConfigurer webFluxConfigurer(Jackson2JsonEncoder encoder, Jackson2JsonDecoder decoder){

return new WebFluxConfigurer() {

@Override

public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {

configurer.defaultCodecs().jackson2Encoder(encoder);

configurer.defaultCodecs().jackson2Decoder(decoder);

}

};

}

}

以上是 在spring-boot-webflux中不使用已配置的ObjectMapper 的全部内容, 来源链接: utcz.com/qa/431737.html

回到顶部