Skip to content

Commit 7baf33f

Browse files
committed
Avoid multiple warnings related to jackson-module-kotlin
Issue: SPR-16497
1 parent 1aeae5d commit 7baf33f

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
*/
9999
public class Jackson2ObjectMapperBuilder {
100100

101+
private static volatile boolean kotlinWarningLogged = false;
102+
101103
private final Log logger = LogFactory.getLog(getClass());
102104

103105
private final Map<Class<?>, Class<?>> mixIns = new HashMap<>();
@@ -543,7 +545,7 @@ public Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends Module>... m
543545

544546
/**
545547
* Set whether to let Jackson find available modules via the JDK ServiceLoader,
546-
* based on META-INF metadata in the classpath. Requires Jackson 2.2 or higher.
548+
* based on META-INF metadata in the classpath.
547549
* <p>If this mode is not set, Spring's Jackson2ObjectMapperBuilder itself
548550
* will try to find the JSR-310 and Joda-Time support modules on the classpath -
549551
* provided that Java 8 and Joda-Time themselves are available, respectively.
@@ -616,18 +618,14 @@ public void configure(ObjectMapper objectMapper) {
616618
Assert.notNull(objectMapper, "ObjectMapper must not be null");
617619

618620
if (this.findModulesViaServiceLoader) {
619-
// Jackson 2.2+
620621
objectMapper.registerModules(ObjectMapper.findModules(this.moduleClassLoader));
621622
}
622623
else if (this.findWellKnownModules) {
623624
registerWellKnownModulesIfAvailable(objectMapper);
624625
}
625626

626627
if (this.modules != null) {
627-
for (Module module : this.modules) {
628-
// Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules
629-
objectMapper.registerModule(module);
630-
}
628+
objectMapper.registerModules(this.modules);
631629
}
632630
if (this.moduleClasses != null) {
633631
for (Class<? extends Module> module : this.moduleClasses) {
@@ -770,13 +768,15 @@ private void registerWellKnownModulesIfAvailable(ObjectMapper objectMapper) {
770768
if (KotlinDetector.isKotlinPresent()) {
771769
try {
772770
Class<? extends Module> kotlinModule = (Class<? extends Module>)
773-
ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule",
774-
this.moduleClassLoader);
771+
ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", this.moduleClassLoader);
775772
objectMapper.registerModule(BeanUtils.instantiateClass(kotlinModule));
776773
}
777774
catch (ClassNotFoundException ex) {
778-
logger.warn("For Jackson Kotlin classes support please add " +
779-
"\"com.fasterxml.jackson.module:jackson-module-kotlin\" to the classpath");
775+
if (!kotlinWarningLogged) {
776+
kotlinWarningLogged = true;
777+
logger.warn("For Jackson Kotlin classes support please add " +
778+
"\"com.fasterxml.jackson.module:jackson-module-kotlin\" to the classpath");
779+
}
780780
}
781781
}
782782
}

0 commit comments

Comments
 (0)