Skip to content

Commit e50131d

Browse files
committed
Add Consumer methods to configure Jackson modules
Closes gh-28633
1 parent cdd4e8c commit e50131d

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

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

+37-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -521,7 +521,7 @@ public Jackson2ObjectMapperBuilder featuresToDisable(Object... featuresToDisable
521521
}
522522

523523
/**
524-
* Specify one or more modules to be registered with the {@link ObjectMapper}.
524+
* Specify the modules to be registered with the {@link ObjectMapper}.
525525
* <p>Multiple invocations are not additive, the last one defines the modules to
526526
* register.
527527
* <p>Note: If this is set, no finding of modules is going to happen - not by
@@ -538,15 +538,9 @@ public Jackson2ObjectMapperBuilder modules(Module... modules) {
538538
}
539539

540540
/**
541-
* Set a complete list of modules to be registered with the {@link ObjectMapper}.
542-
* <p>Multiple invocations are not additive, the last one defines the modules to
543-
* register.
544-
* <p>Note: If this is set, no finding of modules is going to happen - not by
545-
* Jackson, and not by Spring either (see {@link #findModulesViaServiceLoader}).
546-
* As a consequence, specifying an empty list here will suppress any kind of
547-
* module detection.
548-
* <p>Specify either this or {@link #modulesToInstall}, not both.
541+
* Variant of {@link #modules(Module...)} with a {@link List}.
549542
* @see #modules(Module...)
543+
* @see #modules(Consumer)
550544
* @see com.fasterxml.jackson.databind.Module
551545
*/
552546
public Jackson2ObjectMapperBuilder modules(List<Module> modules) {
@@ -556,6 +550,22 @@ public Jackson2ObjectMapperBuilder modules(List<Module> modules) {
556550
return this;
557551
}
558552

553+
/**
554+
* Variant of {@link #modules(Module...)} with a {@link Consumer} for full
555+
* control over the underlying list of modules.
556+
* @since 6.0
557+
* @see #modules(Module...)
558+
* @see #modules(List)
559+
* @see com.fasterxml.jackson.databind.Module
560+
*/
561+
public Jackson2ObjectMapperBuilder modules(Consumer<List<Module>> consumer) {
562+
this.modules = (this.modules != null ? this.modules : new ArrayList<>());
563+
this.findModulesViaServiceLoader = false;
564+
this.findWellKnownModules = false;
565+
consumer.accept(this.modules);
566+
return this;
567+
}
568+
559569
/**
560570
* Specify one or more modules to be registered with the {@link ObjectMapper}.
561571
* <p>Multiple invocations are not additive, the last one defines the modules
@@ -566,6 +576,7 @@ public Jackson2ObjectMapperBuilder modules(List<Module> modules) {
566576
* allowing to eventually override their configuration.
567577
* <p>Specify either this or {@link #modules(Module...)}, not both.
568578
* @since 4.1.5
579+
* @see #modulesToInstall(Consumer)
569580
* @see #modulesToInstall(Class...)
570581
* @see com.fasterxml.jackson.databind.Module
571582
*/
@@ -575,6 +586,21 @@ public Jackson2ObjectMapperBuilder modulesToInstall(Module... modules) {
575586
return this;
576587
}
577588

589+
/**
590+
* Variant of {@link #modulesToInstall(Module...)} with a {@link Consumer}
591+
* for full control over the underlying list of modules.
592+
* @since 6.0
593+
* @see #modulesToInstall(Module...)
594+
* @see #modulesToInstall(Class...)
595+
* @see com.fasterxml.jackson.databind.Module
596+
*/
597+
public Jackson2ObjectMapperBuilder modulesToInstall(Consumer<List<Module>> consumer) {
598+
this.modules = (this.modules != null ? this.modules : new ArrayList<>());
599+
this.findWellKnownModules = true;
600+
consumer.accept(this.modules);
601+
return this;
602+
}
603+
578604
/**
579605
* Specify one or more modules by class to be registered with
580606
* the {@link ObjectMapper}.
@@ -586,6 +612,7 @@ public Jackson2ObjectMapperBuilder modulesToInstall(Module... modules) {
586612
* allowing to eventually override their configuration.
587613
* <p>Specify either this or {@link #modules(Module...)}, not both.
588614
* @see #modulesToInstall(Module...)
615+
* @see #modulesToInstall(Consumer)
589616
* @see com.fasterxml.jackson.databind.Module
590617
*/
591618
@SafeVarargs

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -241,6 +241,16 @@ void modules() {
241241
assertThat(serializers.findSerializer(null, SimpleType.construct(Integer.class), null)).isSameAs(serializer1);
242242
}
243243

244+
@Test
245+
void modulesWithConsumer() {
246+
NumberSerializer serializer1 = new NumberSerializer(Integer.class);
247+
SimpleModule module = new SimpleModule();
248+
module.addSerializer(Integer.class, serializer1);
249+
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(list -> list.add(module) ).build();
250+
Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
251+
assertThat(serializers.findSerializer(null, SimpleType.construct(Integer.class), null)).isSameAs(serializer1);
252+
}
253+
244254
@Test
245255
void modulesToInstallByClass() {
246256
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
@@ -259,6 +269,15 @@ void modulesToInstallByInstance() {
259269
assertThat(serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass()).isSameAs(CustomIntegerSerializer.class);
260270
}
261271

272+
@Test
273+
void modulesToInstallWithConsumer() {
274+
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
275+
.modulesToInstall(list -> list.add(new CustomIntegerModule()))
276+
.build();
277+
Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
278+
assertThat(serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass()).isSameAs(CustomIntegerSerializer.class);
279+
}
280+
262281
@Test
263282
void wellKnownModules() throws JsonProcessingException, UnsupportedEncodingException {
264283
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();

0 commit comments

Comments
 (0)