Add shortcuts for Jackson mix-in annotations registration

This commit adds support for direct Jackson mix-in annotations registration in
Jackson2ObjectMapperFactoryBean and Jackson2ObjectMapperBuilder.

Issue: SPR-12144
This commit is contained in:
Tadaya Tsuyukubo
2014-09-02 23:17:32 -07:00
committed by Sebastien Deleuze
parent 5a631177ff
commit 25bb58a1e8
4 changed files with 62 additions and 0 deletions

View File

@@ -60,6 +60,7 @@ import org.springframework.util.ClassUtils;
*
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @author Tadaya Tsuyukubo
* @since 4.1.1
* @see #build()
* @see #configure(ObjectMapper)
@@ -81,6 +82,8 @@ public class Jackson2ObjectMapperBuilder {
private final Map<Class<?>, JsonDeserializer<?>> deserializers = new LinkedHashMap<Class<?>, JsonDeserializer<?>>();
private final Map<Class<?>, Class<?>> mixIns = new HashMap<Class<?>, Class<?>>();
private final Map<Object, Boolean> features = new HashMap<Object, Boolean>();
private List<Module> modules;
@@ -190,6 +193,21 @@ public class Jackson2ObjectMapperBuilder {
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<?>, Class<?>> mixIns) {
if (mixIns != null) {
this.mixIns.putAll(mixIns);
}
return this;
}
/**
* Shortcut for {@link MapperFeature#AUTO_DETECT_FIELDS} option.
*/
@@ -414,6 +432,9 @@ public class Jackson2ObjectMapperBuilder {
if (this.propertyNamingStrategy != null) {
objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
}
for (Class<?> target : this.mixIns.keySet()) {
objectMapper.addMixInAnnotations(target, this.mixIns.get(target));
}
}
@SuppressWarnings("unchecked")

View File

@@ -118,6 +118,7 @@ import org.springframework.beans.factory.InitializingBean;
* @author Rossen Stoyanchev
* @author Brian Clozel
* @author Juergen Hoeller
* @author Tadaya Tsuyukubo
* @since 3.2
*/
public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper>, BeanClassLoaderAware, InitializingBean {
@@ -213,6 +214,18 @@ 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.
*/