Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

492
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda