/
Jackson2ObjectMapperBuilder.java
920 lines (808 loc) · 31.7 KB
/
Jackson2ObjectMapperBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.converter.json;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Function;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.commons.logging.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import org.springframework.context.ApplicationContext;
import org.springframework.core.KotlinDetector;
import org.springframework.http.HttpLogging;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.StaxUtils;
/**
* A builder used to create {@link ObjectMapper} instances with a fluent API.
*
* <p>It customizes Jackson's default properties with the following ones:
* <ul>
* <li>{@link MapperFeature#DEFAULT_VIEW_INCLUSION} is disabled</li>
* <li>{@link DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES} is disabled</li>
* </ul>
*
* <p>It also automatically registers the following well-known modules if they are
* detected on the classpath:
* <ul>
* <li><a href="https://github.com/FasterXML/jackson-datatype-jdk8">jackson-datatype-jdk8</a>:
* support for other Java 8 types like {@link java.util.Optional}</li>
* <li><a href="https://github.com/FasterXML/jackson-datatype-jsr310">jackson-datatype-jsr310</a>:
* support for Java 8 Date & Time API types</li>
* <li><a href="https://github.com/FasterXML/jackson-datatype-joda">jackson-datatype-joda</a>:
* support for Joda-Time types</li>
* <li><a href="https://github.com/FasterXML/jackson-module-kotlin">jackson-module-kotlin</a>:
* support for Kotlin classes and data classes</li>
* </ul>
*
* <p>Compatible with Jackson 2.6 and higher, as of Spring 4.3.
*
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @author Tadaya Tsuyukubo
* @author Eddú Meléndez
* @since 4.1.1
* @see #build()
* @see #configure(ObjectMapper)
* @see Jackson2ObjectMapperFactoryBean
*/
public class Jackson2ObjectMapperBuilder {
private static volatile boolean kotlinWarningLogged = false;
private final Log logger = HttpLogging.forLogName(getClass());
private final Map<Class<?>, Class<?>> mixIns = new LinkedHashMap<>();
private final Map<Class<?>, JsonSerializer<?>> serializers = new LinkedHashMap<>();
private final Map<Class<?>, JsonDeserializer<?>> deserializers = new LinkedHashMap<>();
private final Map<PropertyAccessor, JsonAutoDetect.Visibility> visibilities = new LinkedHashMap<>();
private final Map<Object, Boolean> features = new LinkedHashMap<>();
private boolean createXmlMapper = false;
@Nullable
private JsonFactory factory;
@Nullable
private DateFormat dateFormat;
@Nullable
private Locale locale;
@Nullable
private TimeZone timeZone;
@Nullable
private AnnotationIntrospector annotationIntrospector;
@Nullable
private PropertyNamingStrategy propertyNamingStrategy;
@Nullable
private TypeResolverBuilder<?> defaultTyping;
@Nullable
private JsonInclude.Include serializationInclusion;
@Nullable
private FilterProvider filters;
@Nullable
private List<Module> modules;
@Nullable
private Class<? extends Module>[] moduleClasses;
private boolean findModulesViaServiceLoader = false;
private boolean findWellKnownModules = true;
private ClassLoader moduleClassLoader = getClass().getClassLoader();
@Nullable
private HandlerInstantiator handlerInstantiator;
@Nullable
private ApplicationContext applicationContext;
@Nullable
private Boolean defaultUseWrapper;
/**
* If set to {@code true}, an {@link XmlMapper} will be created using its
* default constructor. This is only applicable to {@link #build()} calls,
* not to {@link #configure} calls.
*/
public Jackson2ObjectMapperBuilder createXmlMapper(boolean createXmlMapper) {
this.createXmlMapper = createXmlMapper;
return this;
}
/**
* Define the {@link JsonFactory} to be used to create the {@link ObjectMapper}
* instance.
* @since 5.0
*/
public Jackson2ObjectMapperBuilder factory(JsonFactory factory) {
this.factory = factory;
return this;
}
/**
* Define the format for date/time with the given {@link DateFormat}.
* <p>Note: Setting this property makes the exposed {@link ObjectMapper}
* non-thread-safe, according to Jackson's thread safety rules.
* @see #simpleDateFormat(String)
*/
public Jackson2ObjectMapperBuilder dateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
return this;
}
/**
* Define the date/time format with a {@link SimpleDateFormat}.
* <p>Note: Setting this property makes the exposed {@link ObjectMapper}
* non-thread-safe, according to Jackson's thread safety rules.
* @see #dateFormat(DateFormat)
*/
public Jackson2ObjectMapperBuilder simpleDateFormat(String format) {
this.dateFormat = new SimpleDateFormat(format);
return this;
}
/**
* Override the default {@link Locale} to use for formatting.
* Default value used is {@link Locale#getDefault()}.
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder locale(Locale locale) {
this.locale = locale;
return this;
}
/**
* Override the default {@link Locale} to use for formatting.
* Default value used is {@link Locale#getDefault()}.
* @param localeString the locale ID as a String representation
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder locale(String localeString) {
this.locale = StringUtils.parseLocale(localeString);
return this;
}
/**
* Override the default {@link TimeZone} to use for formatting.
* Default value used is UTC (NOT local timezone).
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder timeZone(TimeZone timeZone) {
this.timeZone = timeZone;
return this;
}
/**
* Override the default {@link TimeZone} to use for formatting.
* Default value used is UTC (NOT local timezone).
* @param timeZoneString the zone ID as a String representation
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder timeZone(String timeZoneString) {
this.timeZone = StringUtils.parseTimeZoneString(timeZoneString);
return this;
}
/**
* Set an {@link AnnotationIntrospector} for both serialization and deserialization.
*/
public Jackson2ObjectMapperBuilder annotationIntrospector(AnnotationIntrospector annotationIntrospector) {
this.annotationIntrospector = annotationIntrospector;
return this;
}
/**
* Alternative to {@link #annotationIntrospector(AnnotationIntrospector)}
* that allows combining with rather than replacing the currently set
* introspector, e.g. via
* {@link AnnotationIntrospectorPair#pair(AnnotationIntrospector, AnnotationIntrospector)}.
* @param pairingFunction a function to apply to the currently set
* introspector (possibly {@code null}); the result of the function becomes
* the new introspector.
* @since 5.2.4
*/
public Jackson2ObjectMapperBuilder annotationIntrospector(
Function<AnnotationIntrospector, AnnotationIntrospector> pairingFunction) {
this.annotationIntrospector = pairingFunction.apply(this.annotationIntrospector);
return this;
}
/**
* Specify a {@link com.fasterxml.jackson.databind.PropertyNamingStrategy} to
* configure the {@link ObjectMapper} with.
*/
public Jackson2ObjectMapperBuilder propertyNamingStrategy(PropertyNamingStrategy propertyNamingStrategy) {
this.propertyNamingStrategy = propertyNamingStrategy;
return this;
}
/**
* Specify a {@link TypeResolverBuilder} to use for Jackson's default typing.
* @since 4.2.2
*/
public Jackson2ObjectMapperBuilder defaultTyping(TypeResolverBuilder<?> typeResolverBuilder) {
this.defaultTyping = typeResolverBuilder;
return this;
}
/**
* Set a custom inclusion strategy for serialization.
* @see com.fasterxml.jackson.annotation.JsonInclude.Include
*/
public Jackson2ObjectMapperBuilder serializationInclusion(JsonInclude.Include serializationInclusion) {
this.serializationInclusion = serializationInclusion;
return this;
}
/**
* Set the global filters to use in order to support {@link JsonFilter @JsonFilter} annotated POJO.
* @since 4.2
* @see MappingJacksonValue#setFilters(FilterProvider)
*/
public Jackson2ObjectMapperBuilder filters(FilterProvider filters) {
this.filters = filters;
return this;
}
/**
* Add mix-in annotations to use for augmenting specified class or interface.
* @param target class (or interface) whose annotations to effectively override
* @param mixinSource class (or interface) whose annotations are to be "added"
* to target's annotations as value
* @since 4.1.2
* @see com.fasterxml.jackson.databind.ObjectMapper#addMixIn(Class, Class)
*/
public Jackson2ObjectMapperBuilder mixIn(Class<?> target, Class<?> mixinSource) {
this.mixIns.put(target, mixinSource);
return this;
}
/**
* Add mix-in annotations to use for augmenting specified class or interface.
* @param mixIns a Map of entries with target classes (or interface) whose annotations
* to effectively override as key and mix-in classes (or interface) whose
* annotations are to be "added" to target's annotations as value.
* @since 4.1.2
* @see com.fasterxml.jackson.databind.ObjectMapper#addMixIn(Class, Class)
*/
public Jackson2ObjectMapperBuilder mixIns(Map<Class<?>, Class<?>> mixIns) {
this.mixIns.putAll(mixIns);
return this;
}
/**
* Configure custom serializers. Each serializer is registered for the type
* returned by {@link JsonSerializer#handledType()}, which must not be {@code null}.
* @see #serializersByType(Map)
*/
public Jackson2ObjectMapperBuilder serializers(JsonSerializer<?>... serializers) {
for (JsonSerializer<?> serializer : serializers) {
Class<?> handledType = serializer.handledType();
if (handledType == null || handledType == Object.class) {
throw new IllegalArgumentException("Unknown handled type in " + serializer.getClass().getName());
}
this.serializers.put(serializer.handledType(), serializer);
}
return this;
}
/**
* Configure a custom serializer for the given type.
* @since 4.1.2
* @see #serializers(JsonSerializer...)
*/
public Jackson2ObjectMapperBuilder serializerByType(Class<?> type, JsonSerializer<?> serializer) {
this.serializers.put(type, serializer);
return this;
}
/**
* Configure custom serializers for the given types.
* @see #serializers(JsonSerializer...)
*/
public Jackson2ObjectMapperBuilder serializersByType(Map<Class<?>, JsonSerializer<?>> serializers) {
this.serializers.putAll(serializers);
return this;
}
/**
* Configure custom deserializers. Each deserializer is registered for the type
* returned by {@link JsonDeserializer#handledType()}, which must not be {@code null}.
* @since 4.3
* @see #deserializersByType(Map)
*/
public Jackson2ObjectMapperBuilder deserializers(JsonDeserializer<?>... deserializers) {
for (JsonDeserializer<?> deserializer : deserializers) {
Class<?> handledType = deserializer.handledType();
if (handledType == null || handledType == Object.class) {
throw new IllegalArgumentException("Unknown handled type in " + deserializer.getClass().getName());
}
this.deserializers.put(deserializer.handledType(), deserializer);
}
return this;
}
/**
* Configure a custom deserializer for the given type.
* @since 4.1.2
*/
public Jackson2ObjectMapperBuilder deserializerByType(Class<?> type, JsonDeserializer<?> deserializer) {
this.deserializers.put(type, deserializer);
return this;
}
/**
* Configure custom deserializers for the given types.
*/
public Jackson2ObjectMapperBuilder deserializersByType(Map<Class<?>, JsonDeserializer<?>> deserializers) {
this.deserializers.putAll(deserializers);
return this;
}
/**
* Shortcut for {@link MapperFeature#AUTO_DETECT_FIELDS} option.
*/
public Jackson2ObjectMapperBuilder autoDetectFields(boolean autoDetectFields) {
this.features.put(MapperFeature.AUTO_DETECT_FIELDS, autoDetectFields);
return this;
}
/**
* Shortcut for {@link MapperFeature#AUTO_DETECT_SETTERS}/
* {@link MapperFeature#AUTO_DETECT_GETTERS}/{@link MapperFeature#AUTO_DETECT_IS_GETTERS}
* options.
*/
public Jackson2ObjectMapperBuilder autoDetectGettersSetters(boolean autoDetectGettersSetters) {
this.features.put(MapperFeature.AUTO_DETECT_GETTERS, autoDetectGettersSetters);
this.features.put(MapperFeature.AUTO_DETECT_SETTERS, autoDetectGettersSetters);
this.features.put(MapperFeature.AUTO_DETECT_IS_GETTERS, autoDetectGettersSetters);
return this;
}
/**
* Shortcut for {@link MapperFeature#DEFAULT_VIEW_INCLUSION} option.
*/
public Jackson2ObjectMapperBuilder defaultViewInclusion(boolean defaultViewInclusion) {
this.features.put(MapperFeature.DEFAULT_VIEW_INCLUSION, defaultViewInclusion);
return this;
}
/**
* Shortcut for {@link DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES} option.
*/
public Jackson2ObjectMapperBuilder failOnUnknownProperties(boolean failOnUnknownProperties) {
this.features.put(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
return this;
}
/**
* Shortcut for {@link SerializationFeature#FAIL_ON_EMPTY_BEANS} option.
*/
public Jackson2ObjectMapperBuilder failOnEmptyBeans(boolean failOnEmptyBeans) {
this.features.put(SerializationFeature.FAIL_ON_EMPTY_BEANS, failOnEmptyBeans);
return this;
}
/**
* Shortcut for {@link SerializationFeature#INDENT_OUTPUT} option.
*/
public Jackson2ObjectMapperBuilder indentOutput(boolean indentOutput) {
this.features.put(SerializationFeature.INDENT_OUTPUT, indentOutput);
return this;
}
/**
* Define if a wrapper will be used for indexed (List, array) properties or not by
* default (only applies to {@link XmlMapper}).
* @since 4.3
*/
public Jackson2ObjectMapperBuilder defaultUseWrapper(boolean defaultUseWrapper) {
this.defaultUseWrapper = defaultUseWrapper;
return this;
}
/**
* Specify visibility to limit what kind of properties are auto-detected.
* @since 5.1
* @see com.fasterxml.jackson.annotation.PropertyAccessor
* @see com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility
*/
public Jackson2ObjectMapperBuilder visibility(PropertyAccessor accessor, JsonAutoDetect.Visibility visibility) {
this.visibilities.put(accessor, visibility);
return this;
}
/**
* Specify features to enable.
* @see com.fasterxml.jackson.core.JsonParser.Feature
* @see com.fasterxml.jackson.core.JsonGenerator.Feature
* @see com.fasterxml.jackson.databind.SerializationFeature
* @see com.fasterxml.jackson.databind.DeserializationFeature
* @see com.fasterxml.jackson.databind.MapperFeature
*/
public Jackson2ObjectMapperBuilder featuresToEnable(Object... featuresToEnable) {
for (Object feature : featuresToEnable) {
this.features.put(feature, Boolean.TRUE);
}
return this;
}
/**
* Specify features to disable.
* @see com.fasterxml.jackson.core.JsonParser.Feature
* @see com.fasterxml.jackson.core.JsonGenerator.Feature
* @see com.fasterxml.jackson.databind.SerializationFeature
* @see com.fasterxml.jackson.databind.DeserializationFeature
* @see com.fasterxml.jackson.databind.MapperFeature
*/
public Jackson2ObjectMapperBuilder featuresToDisable(Object... featuresToDisable) {
for (Object feature : featuresToDisable) {
this.features.put(feature, Boolean.FALSE);
}
return this;
}
/**
* Specify one or more modules to be registered with the {@link ObjectMapper}.
* Multiple invocations are not additive, the last one defines the modules to
* register.
* <p>Note: If this is set, no finding of modules is going to happen - not by
* Jackson, and not by Spring either (see {@link #findModulesViaServiceLoader}).
* As a consequence, specifying an empty list here will suppress any kind of
* module detection.
* <p>Specify either this or {@link #modulesToInstall}, not both.
* @since 4.1.5
* @see #modules(List)
* @see com.fasterxml.jackson.databind.Module
*/
public Jackson2ObjectMapperBuilder modules(Module... modules) {
return modules(Arrays.asList(modules));
}
/**
* Set a complete list of modules to be registered with the {@link ObjectMapper}.
* Multiple invocations are not additive, the last one defines the modules to
* register.
* <p>Note: If this is set, no finding of modules is going to happen - not by
* Jackson, and not by Spring either (see {@link #findModulesViaServiceLoader}).
* As a consequence, specifying an empty list here will suppress any kind of
* module detection.
* <p>Specify either this or {@link #modulesToInstall}, not both.
* @see #modules(Module...)
* @see com.fasterxml.jackson.databind.Module
*/
public Jackson2ObjectMapperBuilder modules(List<Module> modules) {
this.modules = new LinkedList<>(modules);
this.findModulesViaServiceLoader = false;
this.findWellKnownModules = false;
return this;
}
/**
* Specify one or more modules to be registered with the {@link ObjectMapper}.
* Multiple invocations are not additive, the last one defines the modules
* to register.
* <p>Modules specified here will be registered after
* Spring's autodetection of JSR-310 and Joda-Time, or Jackson's
* finding of modules (see {@link #findModulesViaServiceLoader}),
* allowing to eventually override their configuration.
* <p>Specify either this or {@link #modules}, not both.
* @since 4.1.5
* @see com.fasterxml.jackson.databind.Module
*/
public Jackson2ObjectMapperBuilder modulesToInstall(Module... modules) {
this.modules = Arrays.asList(modules);
this.findWellKnownModules = true;
return this;
}
/**
* Specify one or more modules by class to be registered with
* the {@link ObjectMapper}. Multiple invocations are not additive,
* the last one defines the modules to register.
* <p>Modules specified here will be registered after
* Spring's autodetection of JSR-310 and Joda-Time, or Jackson's
* finding of modules (see {@link #findModulesViaServiceLoader}),
* allowing to eventually override their configuration.
* <p>Specify either this or {@link #modules}, not both.
* @see #modulesToInstall(Module...)
* @see com.fasterxml.jackson.databind.Module
*/
@SuppressWarnings("unchecked")
public Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends Module>... modules) {
this.moduleClasses = modules;
this.findWellKnownModules = true;
return this;
}
/**
* Set whether to let Jackson find available modules via the JDK ServiceLoader,
* based on META-INF metadata in the classpath.
* <p>If this mode is not set, Spring's Jackson2ObjectMapperBuilder itself
* will try to find the JSR-310 and Joda-Time support modules on the classpath -
* provided that Java 8 and Joda-Time themselves are available, respectively.
* @see com.fasterxml.jackson.databind.ObjectMapper#findModules()
*/
public Jackson2ObjectMapperBuilder findModulesViaServiceLoader(boolean findModules) {
this.findModulesViaServiceLoader = findModules;
return this;
}
/**
* Set the ClassLoader to use for loading Jackson extension modules.
*/
public Jackson2ObjectMapperBuilder moduleClassLoader(ClassLoader moduleClassLoader) {
this.moduleClassLoader = moduleClassLoader;
return this;
}
/**
* Customize the construction of Jackson handlers ({@link JsonSerializer}, {@link JsonDeserializer},
* {@link KeyDeserializer}, {@code TypeResolverBuilder} and {@code TypeIdResolver}).
* @since 4.1.3
* @see Jackson2ObjectMapperBuilder#applicationContext(ApplicationContext)
*/
public Jackson2ObjectMapperBuilder handlerInstantiator(HandlerInstantiator handlerInstantiator) {
this.handlerInstantiator = handlerInstantiator;
return this;
}
/**
* Set the Spring {@link ApplicationContext} in order to autowire Jackson handlers ({@link JsonSerializer},
* {@link JsonDeserializer}, {@link KeyDeserializer}, {@code TypeResolverBuilder} and {@code TypeIdResolver}).
* @since 4.1.3
* @see SpringHandlerInstantiator
*/
public Jackson2ObjectMapperBuilder applicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
return this;
}
/**
* Build a new {@link ObjectMapper} instance.
* <p>Each build operation produces an independent {@link ObjectMapper} instance.
* The builder's settings can get modified, with a subsequent build operation
* then producing a new {@link ObjectMapper} based on the most recent settings.
* @return the newly built ObjectMapper
*/
@SuppressWarnings("unchecked")
public <T extends ObjectMapper> T build() {
ObjectMapper mapper;
if (this.createXmlMapper) {
mapper = (this.defaultUseWrapper != null ?
new XmlObjectMapperInitializer().create(this.defaultUseWrapper, this.factory) :
new XmlObjectMapperInitializer().create(this.factory));
}
else {
mapper = (this.factory != null ? new ObjectMapper(this.factory) : new ObjectMapper());
}
configure(mapper);
return (T) mapper;
}
/**
* Configure an existing {@link ObjectMapper} instance with this builder's
* settings. This can be applied to any number of {@code ObjectMappers}.
* @param objectMapper the ObjectMapper to configure
*/
public void configure(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "ObjectMapper must not be null");
MultiValueMap<Object, Module> modulesToRegister = new LinkedMultiValueMap<>();
if (this.findModulesViaServiceLoader) {
ObjectMapper.findModules(this.moduleClassLoader).forEach(module -> registerModule(module, modulesToRegister));
}
else if (this.findWellKnownModules) {
registerWellKnownModulesIfAvailable(modulesToRegister);
}
if (this.modules != null) {
this.modules.forEach(module -> registerModule(module, modulesToRegister));
}
if (this.moduleClasses != null) {
for (Class<? extends Module> moduleClass : this.moduleClasses) {
registerModule(BeanUtils.instantiateClass(moduleClass), modulesToRegister);
}
}
List<Module> modules = new ArrayList<>();
for (List<Module> nestedModules : modulesToRegister.values()) {
modules.addAll(nestedModules);
}
objectMapper.registerModules(modules);
if (this.dateFormat != null) {
objectMapper.setDateFormat(this.dateFormat);
}
if (this.locale != null) {
objectMapper.setLocale(this.locale);
}
if (this.timeZone != null) {
objectMapper.setTimeZone(this.timeZone);
}
if (this.annotationIntrospector != null) {
objectMapper.setAnnotationIntrospector(this.annotationIntrospector);
}
if (this.propertyNamingStrategy != null) {
objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
}
if (this.defaultTyping != null) {
objectMapper.setDefaultTyping(this.defaultTyping);
}
if (this.serializationInclusion != null) {
objectMapper.setSerializationInclusion(this.serializationInclusion);
}
if (this.filters != null) {
objectMapper.setFilterProvider(this.filters);
}
this.mixIns.forEach(objectMapper::addMixIn);
if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) {
SimpleModule module = new SimpleModule();
addSerializers(module);
addDeserializers(module);
objectMapper.registerModule(module);
}
this.visibilities.forEach(objectMapper::setVisibility);
customizeDefaultFeatures(objectMapper);
this.features.forEach((feature, enabled) -> configureFeature(objectMapper, feature, enabled));
if (this.handlerInstantiator != null) {
objectMapper.setHandlerInstantiator(this.handlerInstantiator);
}
else if (this.applicationContext != null) {
objectMapper.setHandlerInstantiator(
new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
}
}
private void registerModule(Module module, MultiValueMap<Object, Module> modulesToRegister) {
if (module.getTypeId() == null) {
modulesToRegister.add(SimpleModule.class.getName(), module);
}
else {
modulesToRegister.set(module.getTypeId(), module);
}
}
// Any change to this method should be also applied to spring-jms and spring-messaging
// MappingJackson2MessageConverter default constructors
private void customizeDefaultFeatures(ObjectMapper objectMapper) {
if (!this.features.containsKey(MapperFeature.DEFAULT_VIEW_INCLUSION)) {
configureFeature(objectMapper, MapperFeature.DEFAULT_VIEW_INCLUSION, false);
}
if (!this.features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
configureFeature(objectMapper, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
}
@SuppressWarnings("unchecked")
private <T> void addSerializers(SimpleModule module) {
this.serializers.forEach((type, serializer) ->
module.addSerializer((Class<? extends T>) type, (JsonSerializer<T>) serializer));
}
@SuppressWarnings("unchecked")
private <T> void addDeserializers(SimpleModule module) {
this.deserializers.forEach((type, deserializer) ->
module.addDeserializer((Class<T>) type, (JsonDeserializer<? extends T>) deserializer));
}
private void configureFeature(ObjectMapper objectMapper, Object feature, boolean enabled) {
if (feature instanceof JsonParser.Feature) {
objectMapper.configure((JsonParser.Feature) feature, enabled);
}
else if (feature instanceof JsonGenerator.Feature) {
objectMapper.configure((JsonGenerator.Feature) feature, enabled);
}
else if (feature instanceof SerializationFeature) {
objectMapper.configure((SerializationFeature) feature, enabled);
}
else if (feature instanceof DeserializationFeature) {
objectMapper.configure((DeserializationFeature) feature, enabled);
}
else if (feature instanceof MapperFeature) {
objectMapper.configure((MapperFeature) feature, enabled);
}
else {
throw new FatalBeanException("Unknown feature class: " + feature.getClass().getName());
}
}
@SuppressWarnings("unchecked")
private void registerWellKnownModulesIfAvailable(MultiValueMap<Object, Module> modulesToRegister) {
try {
Class<? extends Module> jdk8ModuleClass = (Class<? extends Module>)
ClassUtils.forName("com.fasterxml.jackson.datatype.jdk8.Jdk8Module", this.moduleClassLoader);
Module jdk8Module = BeanUtils.instantiateClass(jdk8ModuleClass);
modulesToRegister.set(jdk8Module.getTypeId(), jdk8Module);
}
catch (ClassNotFoundException ex) {
// jackson-datatype-jdk8 not available
}
try {
Class<? extends Module> javaTimeModuleClass = (Class<? extends Module>)
ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", this.moduleClassLoader);
Module javaTimeModule = BeanUtils.instantiateClass(javaTimeModuleClass);
modulesToRegister.set(javaTimeModule.getTypeId(), javaTimeModule);
}
catch (ClassNotFoundException ex) {
// jackson-datatype-jsr310 not available
}
// Joda-Time 2.x present?
if (ClassUtils.isPresent("org.joda.time.YearMonth", this.moduleClassLoader)) {
try {
Class<? extends Module> jodaModuleClass = (Class<? extends Module>)
ClassUtils.forName("com.fasterxml.jackson.datatype.joda.JodaModule", this.moduleClassLoader);
Module jodaModule = BeanUtils.instantiateClass(jodaModuleClass);
modulesToRegister.set(jodaModule.getTypeId(), jodaModule);
}
catch (ClassNotFoundException ex) {
// jackson-datatype-joda not available
}
}
// Kotlin present?
if (KotlinDetector.isKotlinPresent()) {
try {
Class<? extends Module> kotlinModuleClass = (Class<? extends Module>)
ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", this.moduleClassLoader);
Module kotlinModule = BeanUtils.instantiateClass(kotlinModuleClass);
modulesToRegister.set(kotlinModule.getTypeId(), kotlinModule);
}
catch (ClassNotFoundException ex) {
if (!kotlinWarningLogged) {
kotlinWarningLogged = true;
logger.warn("For Jackson Kotlin classes support please add " +
"\"com.fasterxml.jackson.module:jackson-module-kotlin\" to the classpath");
}
}
}
}
// Convenience factory methods
/**
* Obtain a {@link Jackson2ObjectMapperBuilder} instance in order to
* build a regular JSON {@link ObjectMapper} instance.
*/
public static Jackson2ObjectMapperBuilder json() {
return new Jackson2ObjectMapperBuilder();
}
/**
* Obtain a {@link Jackson2ObjectMapperBuilder} instance in order to
* build an {@link XmlMapper} instance.
*/
public static Jackson2ObjectMapperBuilder xml() {
return new Jackson2ObjectMapperBuilder().createXmlMapper(true);
}
/**
* Obtain a {@link Jackson2ObjectMapperBuilder} instance in order to
* build a Smile data format {@link ObjectMapper} instance.
* @since 5.0
*/
public static Jackson2ObjectMapperBuilder smile() {
return new Jackson2ObjectMapperBuilder().factory(new SmileFactoryInitializer().create());
}
/**
* Obtain a {@link Jackson2ObjectMapperBuilder} instance in order to
* build a CBOR data format {@link ObjectMapper} instance.
* @since 5.0
*/
public static Jackson2ObjectMapperBuilder cbor() {
return new Jackson2ObjectMapperBuilder().factory(new CborFactoryInitializer().create());
}
private static class XmlObjectMapperInitializer {
public ObjectMapper create(@Nullable JsonFactory factory) {
if (factory != null) {
return new XmlMapper((XmlFactory) factory);
}
else {
return new XmlMapper(StaxUtils.createDefensiveInputFactory());
}
}
public ObjectMapper create(boolean defaultUseWrapper, @Nullable JsonFactory factory) {
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(defaultUseWrapper);
if (factory != null) {
return new XmlMapper((XmlFactory) factory, module);
}
else {
return new XmlMapper(new XmlFactory(StaxUtils.createDefensiveInputFactory()), module);
}
}
}
private static class SmileFactoryInitializer {
public JsonFactory create() {
return new SmileFactory();
}
}
private static class CborFactoryInitializer {
public JsonFactory create() {
return new CBORFactory();
}
}
}