Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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);
  }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!