Revise ObjectMapper construction (#16295)

Previously an ObjectMapper was created multiple times during startup:
two times during bootstrap and one additional time for the first request sent to Keycloak.
Additionally jackson modules, e.g. support for JSR310 java.time types
were not registered event-though they are present on the classpath.

This PR revises the initialization of the ObjectMapper.

- Ensure ObjectMapper is only initialized once
- Ensure that jackson modules on the classpath are properly

Fixes #16295

Signed-off-by: Thomas Darimont <thomas.darimont@googlemail.com>
This commit is contained in:
Thomas Darimont 2024-05-07 16:38:33 +02:00 committed by Marek Posolda
parent 4b194d00be
commit 6ba8b3faa2

View file

@ -40,7 +40,7 @@ public class ObjectMapperResolver implements ContextResolver<ObjectMapper> {
protected ObjectMapper mapper;
public ObjectMapperResolver() {
mapper = createStreamSerializer();
mapper = ObjectMapperInitializer.OBJECT_MAPPER;
}
public static ObjectMapper createStreamSerializer() {
@ -57,6 +57,11 @@ public class ObjectMapperResolver implements ContextResolver<ObjectMapper> {
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
// allow to discover jackson mappers on the classpath
if (Boolean.parseBoolean(System.getProperty("keycloak.jsonEnableJacksonModuleDiscovery", "true"))) {
mapper.findAndRegisterModules();
}
return mapper;
}
@ -64,4 +69,9 @@ public class ObjectMapperResolver implements ContextResolver<ObjectMapper> {
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
private static class ObjectMapperInitializer {
private static final ObjectMapper OBJECT_MAPPER = createStreamSerializer();
}
}