[英]How to serialize java.time.Instant as ISO string in Spring Webflux
How can I configure webflux to serialize java.time (specifically Instant) as a ISO date string?如何配置 webflux 以将 java.time (特别是即时)序列化为 ISO 日期字符串?
With below code I get:使用以下代码,我得到:
{"id":"id","timestamp":1606890022.131813600}
I would like to get我想得到
{"id":"id","timestamp":"2020-12-02T07:20:16.553Z"}
@EnableWebFlux
@SpringBootApplication
public class WebfluxJavaTimeSerializationApplication {
public static void main(String[] args) {
SpringApplication.run(WebfluxJavaTimeSerializationApplication.class, args);
}
@Bean
public RouterFunction<?> routerFunction() {
return route().GET("/resource", request -> ServerResponse.ok().body(Mono.just(new Resource()),Resource.class)).build();
}
public class Resource {
String id = "id";
Instant timestamp = Instant.now();
public String getId() {
return id;
}
public Instant getTimestamp() {
return timestamp;
}
}
}
I've tried configurations like:我尝试过如下配置:
@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {
//moved from application class
@Bean
public RouterFunction<?> routerFunction() {
return route().GET("/resource", request -> ServerResponse.ok().body(Mono.just(new Resource()), Resource.class)).build();
}
@Override
public void addFormatters(FormatterRegistry registry) {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setUseIsoFormat(true);
registrar.registerFormatters(registry);
}
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper));
configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper));
}
}
but it does not change the response.但它不会改变响应。 I believe formatters are only used for deserialization and codecs for the WebClient (but might be mistaken).
我相信格式化程序仅用于 WebClient 的反序列化和编解码器(但可能是错误的)。
You are disabling the auto-configuration of Jackson and WebFlux due to having added @EnableWebFlux
.由于添加了
@EnableWebFlux
,您正在禁用 Jackson 和 WebFlux 的自动配置。 With this Spring Boot will back-off in configuring things, one of those things is a fully pre-configured Jackson.使用此 Spring 引导将在配置时回退,其中之一是完全预配置的 Jackson。
So try not to outsmart the defaults in this case.因此,在这种情况下,尽量不要智取默认值。
Remove the @EnableWebFlux
and also remove the specific formatting for Jackson.删除
@EnableWebFlux
并删除 Jackson 的特定格式。 You could actually remove the implements WebFluxConfigurer
and remove the overridden methods.您实际上可以删除
implements WebFluxConfigurer
并删除覆盖的方法。
I do have the same problem as Streef.我确实和斯特里夫有同样的问题。
In my case implementing the WebFluxConfigurer and using the configureHttpMessageCodecs is what solve the problem.在我的情况下,实现 WebFluxConfigurer 并使用 configureHttpMessageCodecs 是解决问题的方法。
@Configuration
public class JacksonConfiguration implements WebFluxConfigurer {
private final ObjectMapper mapper;
public JacksonConfiguration(ObjectMapper mapper) {
this.mapper = mapper;
}
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
WebFluxConfigurer.super.configureHttpMessageCodecs(configurer);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.