18

我在我的 objectmapperbuilder 配置中配置了 mixins,使用常规的 spring web 控制器,根据 mixins 输出的数据。但是,使用 webflux,具有返回 Flow 或 Mono 的方法的控制器将数据序列化,就像 objectmapper 是默认的一样。

如何让 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);
}
4

5 回答 5

17

我实际上是通过单步执行初始化代码找到了我的解决方案:

@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().jackson2JsonEncoder(encoder);
                configurer.defaultCodecs().jackson2JsonDecoder(decoder);
            }
        };

    }
}
于 2017-04-05T21:01:09.813 回答
12

为方便起见,我将@Alberto Galiana 的解决方案翻译成Java,并注入了配置好的Objectmapper,这样就避免了多次配置:

@Configuration
@RequiredArgsConstructor
public class WebFluxConfig implements WebFluxConfigurer {

    private final ObjectMapper objectMapper;

    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        configurer.defaultCodecs().jackson2JsonEncoder(
            new Jackson2JsonEncoder(objectMapper)
        );

        configurer.defaultCodecs().jackson2JsonDecoder(
            new Jackson2JsonDecoder(objectMapper)
        );
    }
}
于 2018-07-04T13:01:26.850 回答
9

只需实现WebFluxConfigurer并覆盖方法configureHttpMessageCodecs

Spring Boot 2 + Kotlin 的示例代码

@Configuration
@EnableWebFlux
class WebConfiguration : WebFluxConfigurer {

    override fun configureHttpMessageCodecs(configurer: ServerCodecConfigurer) {
        configurer.defaultCodecs().jackson2JsonEncoder(Jackson2JsonEncoder(ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)))

        configurer.defaultCodecs().jackson2JsonDecoder(Jackson2JsonDecoder(ObjectMapper()
                .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)))
    }
}

确保所有要编码/解码的数据类的所有属性都用@JsonProperty注释,即使属性名称在类和 json 数据中相同

data class MyClass(
    @NotNull
    @JsonProperty("id")
    val id: String,

    @NotNull
    @JsonProperty("my_name")
    val name: String)
于 2018-03-19T18:09:33.077 回答
2

就我而言,我试图使用自定义的ObjectMapper同时从我的应用程序的 default 继承所有行为WebClient

我发现我必须使用WebClient.Builder.codecs. 当我使用WebClient.Builder.exchangeStrategies时,提供的覆盖被忽略。不确定这种行为是否特定于 using WebClient.mutate,但这是我发现的唯一有效的解决方案。

WebClient customizedWebClient = webClient.mutate()
                                         .codecs(clientCodecConfigurer -> 
                                                     clientCodecConfigurer.defaultCodecs()
                                                                          .jackson2JsonDecoder(new Jackson2JsonDecoder(customObjectMapper)))
                                         .build();
于 2021-02-03T02:26:54.490 回答
0

我已经尝试了所有不同的解决方案(@Primary @Beanfor ObjectMapperconfigureHttpMessageCodecs()等)。最后对我有用的是指定 MIME 类型。这是一个例子:

@Configuration
class WebConfig: WebFluxConfigurer {
override fun configureHttpMessageCodecs(configurer: ServerCodecConfigurer) {
        val encoder = Jackson2JsonEncoder(objectMapper, MimeTypeUtils.APPLICATION_JSON)
        val decoder = Jackson2JsonDecoder(objectMapper, MimeTypeUtils.APPLICATION_JSON)
        configurer.defaultCodecs().jackson2JsonEncoder(encoder)
        configurer.defaultCodecs().jackson2JsonDecoder(decoder)
    }
}

于 2020-07-09T21:02:15.210 回答