Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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).

like image 824
p.streef Avatar asked Oct 28 '25 10:10

p.streef


2 Answers

You are disabling the auto-configuration of Jackson and WebFlux due to having added @EnableWebFlux. With this Spring Boot will back-off in configuring things, one of those things is a fully pre-configured Jackson.

So try not to outsmart the defaults in this case.

Remove the @EnableWebFlux and also remove the specific formatting for Jackson. You could actually remove the implements WebFluxConfigurer and remove the overridden methods.

like image 95
M. Deinum Avatar answered Oct 29 '25 23:10

M. Deinum


I do have the same problem as Streef.

In my case implementing the WebFluxConfigurer and using the configureHttpMessageCodecs is what solve the problem.

@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);
  }
}
like image 22
Roger Frauca Cordoba Avatar answered Oct 30 '25 01:10

Roger Frauca Cordoba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!