我在我的objectmapperbuilder配置中配置了mixins,使用常规弹簧网络控制器,根据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);
}
答案 0 :(得分:8)
我实际上是通过逐步执行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);
}
};
}
}
答案 1 :(得分:3)
只需实施 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)
答案 2 :(得分:3)
为方便起见,我将@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)
);
}
}
答案 3 :(得分:0)
我尝试了所有不同的解决方案(@Primary @Bean
,ObjectMapper
的{{1}}等)。最后对我有用的是指定MIME类型。这是一个示例:
configureHttpMessageCodecs()
答案 4 :(得分:0)
就我而言,我尝试使用自定义的 ObjectMapper
,同时继承应用默认 WebClient
的所有行为。
我发现我必须使用 WebClient.Builder.codecs
。当我使用 WebClient.Builder.exchangeStrategies
时,提供的覆盖被忽略。不确定此行为是否特定于使用 WebClient.mutate
,但这是我发现有效的唯一解决方案。
WebClient customizedWebClient = webClient.mutate()
.codecs(clientCodecConfigurer ->
clientCodecConfigurer.defaultCodecs()
.jackson2JsonDecoder(new Jackson2JsonDecoder(customObjectMapper)))
.build();