Spring Webflux:如何在ResponseEntityResultHandler中使用自定义杰克逊ObjectWriter?

时间:2018-08-03 09:50:16

标签: java json spring jackson spring-webflux

我需要使用自定义objectWriter,这取决于在webflux应用程序中编写JSON响应的条件。

@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
    /*Few writers can be use there depend on some condtions*/
    ObjectWriter writer = ObjectWriterProvider.getObjectWriter(/**/);
    /*set writer */
    return super.handleResult(exchange, result);        
}

ObjectWriterInjector.set(new Modifier(objectWriter, prettyPrint));

JAX-RS

我有什么方法可以在webflux中使用自定义对象编写器,而不必重写完整的super.handleResult方法?

1 个答案:

答案 0 :(得分:1)

将此bean添加到您的配置类中。您可以自定义ObjectMapper。

@Bean
public Jackson2ObjectMapperBuilder configureObjectMapper() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    ObjectMapper objectMapper = new ObjectMapper();
    //objectMapper.writer(Yourwritter);
    builder.configure(objectMapper);
    return builder;
}