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

Create an ObjectMapperCustomizer to allow tailoring ObjectMapper through media types configuration #1382

Closed
gregturn opened this issue Oct 25, 2020 · 2 comments
Assignees
Labels
in: configuration Configuration and setup in: mediatypes Media type related functionality type: enhancement
Milestone

Comments

@gregturn
Copy link
Contributor

Create a hook for users to customize the ObjectMapper created in the serializer, like this:

public interface ObjectMapperCustomizer extends Consumer<ObjectMapper> {
}

Then, HalConfiguration could add withObjectMapperCustomizer(ObjectMapperCustomizer), allowing users to supply a lambda function.

Jackson2HalModule can then leverage halConfiguration.getObjectMapperCustomizer.accept(mapper) after getting or creating the ObjectMapper.

@toedter
Copy link

toedter commented Oct 26, 2020

Seem like a great idea, I implemented this in https://github.com/toedter/spring-hateoas-jsonapi
Now the object mapper can be customized like

        @Bean
        JsonApiConfiguration jsonApiConfiguration() {
            return new JsonApiConfiguration()
                    .withObjectMapperCustomizer(objectMapper -> {
                        objectMapper.registerModule(new JavaTimeModule());
                        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
                    });
        }

Question: Why would you introduce a new interface instead of just using

withObjectMapperCustomizer(Consumer<ObjectMapper>)

@gregturn
Copy link
Contributor Author

Actually, your suggestion makes sense. I was away from my IDE and couldn't remember if a concrete interface was needed or not. Clearly not.

gregturn added a commit that referenced this issue Oct 27, 2020
Use HalConfiguration to allow applying customizations to the ObjectMapper.
odrotbohm pushed a commit that referenced this issue Oct 27, 2020
Use HalConfiguration to allow applying customizations to the ObjectMapper.

Original pull request: #1383.
odrotbohm added a commit that referenced this issue Oct 27, 2020
Let HalForms register a custom ObjectMapper customizer to allow separate customizations. If HAL and HAL Forms are supposed to be customized the same way, the same customizer can be registered on both instances. Tweak the application of the customizer to happen inside the configuration classes, not on the outside.

Prepare HalFormsConfiguration to become a fully immutable type in 1.3. Javadoc and assertion polish.

Original pull request: #1383.
@odrotbohm odrotbohm added this to the 1.2 GA milestone Oct 27, 2020
@odrotbohm odrotbohm self-assigned this Oct 27, 2020
@odrotbohm odrotbohm added in: configuration Configuration and setup in: mediatypes Media type related functionality type: enhancement labels Oct 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: configuration Configuration and setup in: mediatypes Media type related functionality type: enhancement
Projects
None yet
Development

No branches or pull requests

3 participants