diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java index f23147199bd..a0edfe42d70 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java @@ -65,7 +65,7 @@ import org.springframework.util.StringUtils; *

Note that Jackson's JSR-310 and Joda-Time support modules will be registered automatically * when available (and when Java 8 and Joda-Time themselves are available, respectively). * - *

Tested against Jackson 2.2, 2.3 and 2.4; compatible with Jackson 2.0 and higher. + *

Tested against Jackson 2.2, 2.3, 2.4, 2.5, 2.6; compatible with Jackson 2.0 and higher. * * @author Sebastien Deleuze * @author Juergen Hoeller @@ -91,12 +91,14 @@ public class Jackson2ObjectMapperBuilder { private JsonInclude.Include serializationInclusion; + private FilterProvider filters; + + private final Map, Class> mixIns = new HashMap, Class>(); + private final Map, JsonSerializer> serializers = new LinkedHashMap, JsonSerializer>(); private final Map, JsonDeserializer> deserializers = new LinkedHashMap, JsonDeserializer>(); - private final Map, Class> mixIns = new HashMap, Class>(); - private final Map features = new HashMap(); private List modules; @@ -111,8 +113,6 @@ public class Jackson2ObjectMapperBuilder { private HandlerInstantiator handlerInstantiator; - private FilterProvider filters; - private ApplicationContext applicationContext; @@ -216,6 +216,46 @@ public class Jackson2ObjectMapperBuilder { 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#addMixInAnnotations(Class, Class) + */ + public Jackson2ObjectMapperBuilder mixIn(Class target, Class mixinSource) { + if (mixinSource != null) { + this.mixIns.put(target, mixinSource); + } + return this; + } + + /** + * Add mix-in annotations to use for augmenting specified class or interface. + * @param mixIns 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#addMixInAnnotations(Class, Class) + */ + public Jackson2ObjectMapperBuilder mixIns(Map, Class> mixIns) { + if (mixIns != null) { + 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 @@ -241,7 +281,7 @@ public class Jackson2ObjectMapperBuilder { * @since 4.1.2 */ public Jackson2ObjectMapperBuilder serializerByType(Class type, JsonSerializer serializer) { - if (serializers != null) { + if (serializer != null) { this.serializers.put(type, serializer); } return this; @@ -263,7 +303,7 @@ public class Jackson2ObjectMapperBuilder { * @since 4.1.2 */ public Jackson2ObjectMapperBuilder deserializerByType(Class type, JsonDeserializer deserializer) { - if (deserializers != null) { + if (deserializer != null) { this.deserializers.put(type, deserializer); } return this; @@ -279,36 +319,6 @@ public class Jackson2ObjectMapperBuilder { 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#addMixInAnnotations(Class, Class) - */ - public Jackson2ObjectMapperBuilder mixIn(Class target, Class mixinSource) { - if (mixinSource != null) { - this.mixIns.put(target, mixinSource); - } - return this; - } - - /** - * Add mix-in annotations to use for augmenting specified class or interface. - * @param mixIns 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#addMixInAnnotations(Class, Class) - */ - public Jackson2ObjectMapperBuilder mixIns(Map, Class> mixIns) { - if (mixIns != null) { - this.mixIns.putAll(mixIns); - } - return this; - } - /** * Shortcut for {@link MapperFeature#AUTO_DETECT_FIELDS} option. */ @@ -490,16 +500,6 @@ public class Jackson2ObjectMapperBuilder { 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; - } - /** * Set the Spring {@link ApplicationContext} in order to autowire Jackson handlers ({@link JsonSerializer}, * {@link JsonDeserializer}, {@link KeyDeserializer}, {@code TypeResolverBuilder} and {@code TypeIdResolver}). @@ -580,11 +580,23 @@ public class Jackson2ObjectMapperBuilder { if (this.annotationIntrospector != null) { objectMapper.setAnnotationIntrospector(this.annotationIntrospector); } - + if (this.propertyNamingStrategy != null) { + objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy); + } if (this.serializationInclusion != null) { objectMapper.setSerializationInclusion(this.serializationInclusion); } + if (this.filters != null) { + // Deprecated as of Jackson 2.6, but just in favor of a fluent variant. + objectMapper.setFilters(this.filters); + } + + for (Class target : this.mixIns.keySet()) { + // Deprecated as of Jackson 2.5, but just in favor of a fluent variant. + objectMapper.addMixInAnnotations(target, this.mixIns.get(target)); + } + if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) { SimpleModule module = new SimpleModule(); addSerializers(module); @@ -597,19 +609,9 @@ public class Jackson2ObjectMapperBuilder { configureFeature(objectMapper, feature, this.features.get(feature)); } - if (this.propertyNamingStrategy != null) { - objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy); - } - for (Class target : this.mixIns.keySet()) { - // Deprecated as of Jackson 2.5, but just in favor of a fluent variant. - objectMapper.addMixInAnnotations(target, this.mixIns.get(target)); - } if (this.handlerInstantiator != null) { objectMapper.setHandlerInstantiator(this.handlerInstantiator); } - if (this.filters != null) { - objectMapper.setFilters(this.filters); - } else if (this.applicationContext != null) { objectMapper.setHandlerInstantiator( new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));