Create a builder for Jackson ObjectMapper
Jackson2ObjectMapperBuilder now allows to create ObjectMapper and XmlMapper instances easily thanks to its fluent API. This builder is used in Jackson message converters and views to instantiate default ObjectMapper and XmlMapper. This commit also add a createXmlMapper property to Jackson2ObjectMapperFactoryBean in order to allow to create easily a XmlMapper instance. Issue: SPR-12243
This commit is contained in:
@@ -103,7 +103,7 @@ public abstract class AbstractJackson2View extends AbstractView {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to use the default pretty printer when writing JSON.
|
||||
* Whether to use the default pretty printer when writing the output.
|
||||
* This is a shortcut for setting up an {@code ObjectMapper} as follows:
|
||||
* <pre class="code">
|
||||
* ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -44,6 +45,8 @@ import org.springframework.web.servlet.View;
|
||||
* will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON
|
||||
* alone via {@link #setExtractValueFromSingleKeyModel}.
|
||||
*
|
||||
* <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
|
||||
*
|
||||
* <p>Compatible with Jackson 2.1 and higher.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
@@ -76,10 +79,12 @@ public class MappingJackson2JsonView extends AbstractJackson2View {
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2JsonView}, setting the content type to {@code application/json}.
|
||||
* Construct a new {@code MappingJackson2JsonView} using default configuration
|
||||
* provided by {@link Jackson2ObjectMapperBuilder} and setting the content type
|
||||
* to {@code application/json}.
|
||||
*/
|
||||
public MappingJackson2JsonView() {
|
||||
super(new ObjectMapper(), DEFAULT_CONTENT_TYPE);
|
||||
super(Jackson2ObjectMapperBuilder.json().build(), DEFAULT_CONTENT_TYPE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Map;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.view.json.AbstractJackson2View;
|
||||
@@ -33,6 +34,8 @@ import org.springframework.web.servlet.view.json.AbstractJackson2View;
|
||||
* entry is used. Users can either specify a specific entry in the model via the
|
||||
* {@link #setModelKey(String) sourceKey} property.
|
||||
*
|
||||
* <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
|
||||
*
|
||||
* <p>Compatible with Jackson 2.1 and higher.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
@@ -46,8 +49,13 @@ public class MappingJackson2XmlView extends AbstractJackson2View {
|
||||
private String modelKey;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2XmlView} using default configuration
|
||||
* provided by {@link Jackson2ObjectMapperBuilder} and setting the content type
|
||||
* to {@code application/xml}.
|
||||
*/
|
||||
public MappingJackson2XmlView() {
|
||||
super(new XmlMapper(), DEFAULT_CONTENT_TYPE);
|
||||
super(Jackson2ObjectMapperBuilder.xml().build(), DEFAULT_CONTENT_TYPE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user