Browse Source

Support for Jackson's default typing via TypeResolverBuilder

Issue: SPR-13569
pull/890/head
Juergen Hoeller 9 years ago
parent
commit
59637ee708
  1. 17
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java
  2. 75
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java

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

@ -44,6 +44,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; @@ -44,6 +44,7 @@ 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.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@ -98,6 +99,8 @@ public class Jackson2ObjectMapperBuilder { @@ -98,6 +99,8 @@ public class Jackson2ObjectMapperBuilder {
private PropertyNamingStrategy propertyNamingStrategy;
private TypeResolverBuilder<?> defaultTyping;
private JsonInclude.Include serializationInclusion;
private FilterProvider filters;
@ -216,6 +219,15 @@ public class Jackson2ObjectMapperBuilder { @@ -216,6 +219,15 @@ public class Jackson2ObjectMapperBuilder {
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
@ -523,6 +535,7 @@ public class Jackson2ObjectMapperBuilder { @@ -523,6 +535,7 @@ public class Jackson2ObjectMapperBuilder {
return this;
}
/**
* Build a new {@link ObjectMapper} instance.
* <p>Each build operation produces an independent {@link ObjectMapper} instance.
@ -588,6 +601,9 @@ public class Jackson2ObjectMapperBuilder { @@ -588,6 +601,9 @@ public class Jackson2ObjectMapperBuilder {
if (this.propertyNamingStrategy != null) {
objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
}
if (this.defaultTyping != null) {
objectMapper.setDefaultTyping(this.defaultTyping);
}
if (this.serializationInclusion != null) {
objectMapper.setSerializationInclusion(this.serializationInclusion);
}
@ -623,6 +639,7 @@ public class Jackson2ObjectMapperBuilder { @@ -623,6 +639,7 @@ public class Jackson2ObjectMapperBuilder {
}
}
// Any change to this method should be also applied to spring-jms and spring-messaging
// MappingJackson2MessageConverter default constructors
private void customizeDefaultFeatures(ObjectMapper objectMapper) {

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

@ -36,6 +36,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; @@ -36,6 +36,7 @@ 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.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@ -215,6 +216,14 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -215,6 +216,14 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.builder.propertyNamingStrategy(propertyNamingStrategy);
}
/**
* Specify a {@link TypeResolverBuilder} to use for Jackson's default typing.
* @since 4.2.2
*/
public void setDefaultTyping(TypeResolverBuilder<?> typeResolverBuilder) {
this.builder.defaultTyping(typeResolverBuilder);
}
/**
* Set a custom inclusion strategy for serialization.
* @see com.fasterxml.jackson.annotation.JsonInclude.Include
@ -223,6 +232,27 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -223,6 +232,27 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.builder.serializationInclusion(serializationInclusion);
}
/**
* Set the global filters to use in order to support {@link JsonFilter @JsonFilter} annotated POJO.
* @since 4.2
* @see Jackson2ObjectMapperBuilder#filters(FilterProvider)
*/
public void setFilters(FilterProvider filters) {
this.builder.filters(filters);
}
/**
* 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 void setMixIns(Map<Class<?>, Class<?>> mixIns) {
this.builder.mixIns(mixIns);
}
/**
* Configure custom serializers. Each serializer is registered for the type
* returned by {@link JsonSerializer#handledType()}, which must not be
@ -248,18 +278,6 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -248,18 +278,6 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.builder.deserializersByType(deserializers);
}
/**
* 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 void setMixIns(Map<Class<?>, Class<?>> mixIns) {
this.builder.mixIns(mixIns);
}
/**
* Shortcut for {@link MapperFeature#AUTO_DETECT_FIELDS} option.
*/
@ -373,6 +391,11 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -373,6 +391,11 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.builder.findModulesViaServiceLoader(findModules);
}
@Override
public void setBeanClassLoader(ClassLoader beanClassLoader) {
this.builder.moduleClassLoader(beanClassLoader);
}
/**
* Customize the construction of Jackson handlers ({@link JsonSerializer}, {@link JsonDeserializer},
* {@link KeyDeserializer}, {@code TypeResolverBuilder} and {@code TypeIdResolver}).
@ -384,17 +407,15 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -384,17 +407,15 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
}
/**
* Set the global filters to use in order to support {@link JsonFilter @JsonFilter} annotated POJO.
* @since 4.2
* @see Jackson2ObjectMapperBuilder#filters(FilterProvider)
* Set the builder {@link ApplicationContext} in order to autowire Jackson handlers ({@link JsonSerializer},
* {@link JsonDeserializer}, {@link KeyDeserializer}, {@code TypeResolverBuilder} and {@code TypeIdResolver}).
* @since 4.1.3
* @see Jackson2ObjectMapperBuilder#applicationContext(ApplicationContext)
* @see SpringHandlerInstantiator
*/
public void setFilters(FilterProvider filters) {
this.builder.filters(filters);
}
@Override
public void setBeanClassLoader(ClassLoader beanClassLoader) {
this.builder.moduleClassLoader(beanClassLoader);
public void setApplicationContext(ApplicationContext applicationContext) {
this.builder.applicationContext(applicationContext);
}
@ -408,18 +429,6 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -408,18 +429,6 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
}
}
/**
* Set the builder {@link ApplicationContext} in order to autowire Jackson handlers ({@link JsonSerializer},
* {@link JsonDeserializer}, {@link KeyDeserializer}, {@code TypeResolverBuilder} and {@code TypeIdResolver}).
* @since 4.1.3
* @see Jackson2ObjectMapperBuilder#applicationContext(ApplicationContext)
* @see SpringHandlerInstantiator
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.builder.applicationContext(applicationContext);
}
/**
* Return the singleton ObjectMapper.
*/

Loading…
Cancel
Save