public class MappingJackson2XmlHttpMessageConverter extends org.springframework.http.converter.AbstractHttpMessageConverter<Object>
HttpMessageConverter
that can read
and write JSON using Jackson 2's ObjectMapper
.
This converter can be used to bind to typed beans, or untyped HashMap
instances.
By default, this converter supports application/json
. This can be overridden by setting the
supportedMediaTypes
property.
This converter also support custom JSON views, see RESThub documentation for more details
Modifier and Type | Field and Description |
---|---|
static Charset |
DEFAULT_CHARSET |
Constructor and Description |
---|
MappingJackson2XmlHttpMessageConverter()
Construct a new
BindingJacksonHttpMessageConverter . |
Modifier and Type | Method and Description |
---|---|
boolean |
canRead(Class<?> clazz,
org.springframework.http.MediaType mediaType) |
boolean |
canWrite(Class<?> clazz,
org.springframework.http.MediaType mediaType) |
protected com.fasterxml.jackson.databind.JavaType |
getJavaType(Class<?> clazz)
Return the Jackson
JavaType for the specified class. |
protected com.fasterxml.jackson.core.JsonEncoding |
getJsonEncoding(org.springframework.http.MediaType contentType)
Determine the JSON encoding to use for the given content type.
|
com.fasterxml.jackson.databind.ObjectMapper |
getObjectMapper()
Return the underlying
ObjectMapper for this view. |
protected Object |
readInternal(Class<?> clazz,
org.springframework.http.HttpInputMessage inputMessage) |
void |
setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Set the
ObjectMapper for this view. |
void |
setPrettyPrint(boolean prettyPrint)
Whether to use the
DefaultPrettyPrinter when writing XML. |
protected boolean |
supports(Class<?> clazz) |
protected void |
writeInternal(Object object,
org.springframework.http.HttpOutputMessage outputMessage) |
public static final Charset DEFAULT_CHARSET
public MappingJackson2XmlHttpMessageConverter()
BindingJacksonHttpMessageConverter
.public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
ObjectMapper
for this view. If not set, a default ObjectMapper
is used.
Setting a custom-configured ObjectMapper
is one way to take further control of the JSON serialization
process. For example, an extended SerializerFactory
can be configured that
provides custom serializers for specific types. The other option for refining the serialization process is to use
Jackson's provided annotations on the types to be serialized, in which case a custom-configured ObjectMapper is
unnecessary.
public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
ObjectMapper
for this view.public void setPrettyPrint(boolean prettyPrint)
DefaultPrettyPrinter
when writing XML. This is a shortcut for setting up an
ObjectMapper
as follows:
ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); converter.setObjectMapper(mapper);
public boolean canRead(Class<?> clazz, org.springframework.http.MediaType mediaType)
public boolean canWrite(Class<?> clazz, org.springframework.http.MediaType mediaType)
protected boolean supports(Class<?> clazz)
supports
in class org.springframework.http.converter.AbstractHttpMessageConverter<Object>
protected Object readInternal(Class<?> clazz, org.springframework.http.HttpInputMessage inputMessage) throws IOException, org.springframework.http.converter.HttpMessageNotReadableException
readInternal
in class org.springframework.http.converter.AbstractHttpMessageConverter<Object>
IOException
org.springframework.http.converter.HttpMessageNotReadableException
protected void writeInternal(Object object, org.springframework.http.HttpOutputMessage outputMessage) throws IOException, org.springframework.http.converter.HttpMessageNotWritableException
writeInternal
in class org.springframework.http.converter.AbstractHttpMessageConverter<Object>
IOException
org.springframework.http.converter.HttpMessageNotWritableException
protected com.fasterxml.jackson.databind.JavaType getJavaType(Class<?> clazz)
JavaType
for the specified class.
The default implementation returns ObjectMapper.constructType(java.lang.reflect.Type)
, but this can be
overridden in subclasses, to allow for custom generic collection handling. For instance:
protected JavaType getJavaType(Class<?> clazz) { if (List.class.isAssignableFrom(clazz)) { return objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, MyBean.class); } else { return super.getJavaType(clazz); } }
clazz
- the class to return the java type forprotected com.fasterxml.jackson.core.JsonEncoding getJsonEncoding(org.springframework.http.MediaType contentType)
contentType
- the media type as requested by the callernull
)Copyright © 2009–2014. All rights reserved.