Support for Jackson's default typing via TypeResolverBuilder
Issue: SPR-13569
This commit is contained in:
parent
155bbf5057
commit
59637ee708
|
|
@ -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 {
|
|||
|
||||
private PropertyNamingStrategy propertyNamingStrategy;
|
||||
|
||||
private TypeResolverBuilder<?> defaultTyping;
|
||||
|
||||
private JsonInclude.Include serializationInclusion;
|
||||
|
||||
private FilterProvider filters;
|
||||
|
|
@ -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 {
|
|||
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 {
|
|||
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 {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// Any change to this method should be also applied to spring-jms and spring-messaging
|
||||
// MappingJackson2MessageConverter default constructors
|
||||
private void customizeDefaultFeatures(ObjectMapper 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
|
|||
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
|
|||
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
|
|||
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
|
|||
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}).
|
||||
|
|
@ -383,31 +406,6 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
|
|||
this.builder.handlerInstantiator(handlerInstantiator);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader beanClassLoader) {
|
||||
this.builder.moduleClassLoader(beanClassLoader);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
if (this.objectMapper != null) {
|
||||
this.builder.configure(this.objectMapper);
|
||||
}
|
||||
else {
|
||||
this.objectMapper = this.builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the builder {@link ApplicationContext} in order to autowire Jackson handlers ({@link JsonSerializer},
|
||||
* {@link JsonDeserializer}, {@link KeyDeserializer}, {@code TypeResolverBuilder} and {@code TypeIdResolver}).
|
||||
|
|
@ -420,6 +418,17 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
|
|||
this.builder.applicationContext(applicationContext);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
if (this.objectMapper != null) {
|
||||
this.builder.configure(this.objectMapper);
|
||||
}
|
||||
else {
|
||||
this.objectMapper = this.builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the singleton ObjectMapper.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue