Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable Jackson ObjectMapper in WebFluxConfigurationSupport [SPR-15247] #19812

Closed
spring-projects-issues opened this issue Feb 12, 2017 · 3 comments
Assignees
Labels
in: web type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Feb 12, 2017

Attila Király opened SPR-15247 and commented

Currently WebFluxConfigurationSupport (in its addDefaultHttpMessageReaders and addDefaultHttpMessageWriters methods) creates ObjectMapper-s indirectly through the new Jackson2JsonDecoder() and new Jackson2JsonEncoder() calls.

These ObjectMapper-s are not passed through any hooks which would make it possible to customize them (for example to disable SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).

The only way I could customize it (and still keep the default writers added) was to create a custom WebFluxConfigurer:

@Component
public class Jackson2WebFluxReConfigurer implements WebFluxConfigurer {

  @Override
  public void extendMessageWriters(List<HttpMessageWriter<?>> writers) {
    writers.removeIf(Jackson2ServerHttpMessageWriter.class::isInstance);

    List<Encoder<?>> sseDataEncoders = new ArrayList<>();
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder(
        Jackson2ObjectMapperBuilder.json().featuresToDisable(
            SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build());
    writers.add(new Jackson2ServerHttpMessageWriter(encoder));
    sseDataEncoders.add(encoder);
    HttpMessageWriter<Object> writer = new ServerSentEventHttpMessageWriter(sseDataEncoders);
    writers.add(new Jackson2ServerHttpMessageWriter(writer));
  }
}

It would be great if (for example) WebFluxConfigurer had a callback method to customize the ObjectMapper (or anytlhing equivalent with this).


Affects: 5.0 M4

Issue Links:

  • #19977 Use ServerCodecConfigurer in HandlerStrategies

Referenced from: commits f8a21ab, 4a7218f, dc3851d

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 20, 2017

Rossen Stoyanchev commented

This will definitely require some kind of improvement, thanks for raising it. I'm postponing till RC1 since we need to a broader review of the message reading and writing and how it's configured.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 20, 2017

Attila Király commented

Thanks for looking into this.

It would be also nice if - whatever the improvement will be - it was possible to use that with spring boots jackson support (the spring.jackson.* properties).

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 27, 2017

Rossen Stoyanchev commented

WebFluxConfigurer now exposes a ServerCodecConfigurer with the method exposing it, configureHttpMessageCodecs, essentially taking the place of the addMessageReaders, addMessageWriters, extendMessageReaders, and extendMessageWriters methods. The new configurer makes it easy to replace the built-in Jackson encoder/decoder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants