Add Jackson's Modules registration in Jackson2OMFactoryBean
Prior to this commit, one couldn't configure Jackson's ObjectMapper with (De)SerializerModifiers or advanced configuration features using XML configuration. This commit updates the FactoryBean and adds a setModule method that will register Modules with the ObjectMapper. Note that this commit is only about XML configuration, since this feature was already available with JavaConfig. Issue: SPR-10429
This commit is contained in:
committed by
Rossen Stoyanchev
parent
e91ce23cd0
commit
a5e3916724
@@ -18,8 +18,10 @@ package org.springframework.http.converter.json;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.FatalBeanException;
|
||||
@@ -35,6 +37,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.Module;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
@@ -95,8 +98,22 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
* </bean>
|
||||
* </pre>
|
||||
*
|
||||
* In case you want to configure Jackson's {@link ObjectMapper} with a {@link Module}, you
|
||||
* can register Modules using {@link #setModules(java.util.List)}
|
||||
*
|
||||
* <pre class="code">
|
||||
* <bean class="org.springframework.web.context.support.Jackson2ObjectMapperFactoryBean">
|
||||
* <property name="modules">
|
||||
* <list>
|
||||
* <bean class="org.example.jackson.module.MySampleModule"/>
|
||||
* </list>
|
||||
* </property>
|
||||
* </bean>
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="mailto:dmitry.katsubo@gmail.com">Dmitry Katsubo</a>
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
* @since 3.2
|
||||
*/
|
||||
public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper>, InitializingBean {
|
||||
@@ -105,6 +122,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
|
||||
|
||||
private Map<Object, Boolean> features = new HashMap<Object, Boolean>();
|
||||
|
||||
private final List<Module> modules = new ArrayList<Module>();
|
||||
|
||||
private DateFormat dateFormat;
|
||||
|
||||
private AnnotationIntrospector annotationIntrospector;
|
||||
@@ -258,6 +277,18 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
|
||||
this.serializationInclusion = serializationInclusion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the list of modules to be registered with {@link ObjectMapper}.
|
||||
* @param modules the list of modules
|
||||
* @see com.fasterxml.jackson.databind.Module
|
||||
* @since 4.0
|
||||
*/
|
||||
public void setModules(List<Module> modules) {
|
||||
if(modules != null) {
|
||||
this.modules.addAll(modules);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
if (this.objectMapper == null) {
|
||||
@@ -286,6 +317,10 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
|
||||
if (this.serializationInclusion != null) {
|
||||
this.objectMapper.setSerializationInclusion(this.serializationInclusion);
|
||||
}
|
||||
|
||||
if(!this.modules.isEmpty()) {
|
||||
this.objectMapper.registerModules(this.modules);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
Reference in New Issue
Block a user