Spring Framework

org.springframework.http.converter.json
Class MappingJackson2HttpMessageConverter

java.lang.Object
  extended by org.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
      extended by org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
All Implemented Interfaces:
HttpMessageConverter<java.lang.Object>

public class MappingJackson2HttpMessageConverter
extends AbstractHttpMessageConverter<java.lang.Object>

Implementation of 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.

Since:
3.2
Author:
Arjen Poutsma, Keith Donald
See Also:
MappingJackson2JsonView

Field Summary
static java.nio.charset.Charset DEFAULT_CHARSET
           
 
Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
 
Constructor Summary
MappingJackson2HttpMessageConverter()
          Construct a new BindingJacksonHttpMessageConverter.
 
Method Summary
 boolean canRead(java.lang.Class<?> clazz, MediaType mediaType)
          This implementation checks if the given class is supported, and if the supported media types include the given media type.
 boolean canWrite(java.lang.Class<?> clazz, MediaType mediaType)
          This implementation checks if the given class is supported, and if the supported media types include the given media type.
protected  com.fasterxml.jackson.databind.JavaType getJavaType(java.lang.Class<?> clazz)
          Return the Jackson JavaType for the specified class.
protected  com.fasterxml.jackson.core.JsonEncoding getJsonEncoding(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  java.lang.Object readInternal(java.lang.Class<?> clazz, HttpInputMessage inputMessage)
          Abstract template method that reads the actualy object.
 void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
          Set the ObjectMapper for this view.
 void setPrefixJson(boolean prefixJson)
          Indicate whether the JSON output by this view should be prefixed with "{} &&".
 void setPrettyPrint(boolean prettyPrint)
          Whether to use the DefaultPrettyPrinter when writing JSON.
protected  boolean supports(java.lang.Class<?> clazz)
          Indicates whether the given class is supported by this converter.
protected  void writeInternal(java.lang.Object object, HttpOutputMessage outputMessage)
          Abstract template method that writes the actual body.
 
Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
canRead, canWrite, getContentLength, getDefaultContentType, getSupportedMediaTypes, read, setSupportedMediaTypes, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHARSET

public static final java.nio.charset.Charset DEFAULT_CHARSET
Constructor Detail

MappingJackson2HttpMessageConverter

public MappingJackson2HttpMessageConverter()
Construct a new BindingJacksonHttpMessageConverter.

Method Detail

setObjectMapper

public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Set the 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.


getObjectMapper

public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
Return the underlying ObjectMapper for this view.


setPrefixJson

public void setPrefixJson(boolean prefixJson)
Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false.

Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. This prefix does not affect the evaluation of JSON, but if JSON validation is performed on the string, the prefix would need to be ignored.


setPrettyPrint

public void setPrettyPrint(boolean prettyPrint)
Whether to use the DefaultPrettyPrinter when writing JSON. This is a shortcut for setting up an ObjectMapper as follows:
 ObjectMapper mapper = new ObjectMapper();
 mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
 converter.setObjectMapper(mapper);
 


canRead

public boolean canRead(java.lang.Class<?> clazz,
                       MediaType mediaType)
Description copied from class: AbstractHttpMessageConverter
This implementation checks if the given class is supported, and if the supported media types include the given media type.

Specified by:
canRead in interface HttpMessageConverter<java.lang.Object>
Overrides:
canRead in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the class to test for readability
mediaType - the media type to read, can be null if not specified. Typically the value of a Content-Type header.
Returns:
true if readable; false otherwise

canWrite

public boolean canWrite(java.lang.Class<?> clazz,
                        MediaType mediaType)
Description copied from class: AbstractHttpMessageConverter
This implementation checks if the given class is supported, and if the supported media types include the given media type.

Specified by:
canWrite in interface HttpMessageConverter<java.lang.Object>
Overrides:
canWrite in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the class to test for writability
mediaType - the media type to write, can be null if not specified. Typically the value of an Accept header.
Returns:
true if writable; false otherwise

supports

protected boolean supports(java.lang.Class<?> clazz)
Description copied from class: AbstractHttpMessageConverter
Indicates whether the given class is supported by this converter.

Specified by:
supports in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the class to test for support
Returns:
true if supported; false otherwise

readInternal

protected java.lang.Object readInternal(java.lang.Class<?> clazz,
                                        HttpInputMessage inputMessage)
                                 throws java.io.IOException,
                                        HttpMessageNotReadableException
Description copied from class: AbstractHttpMessageConverter
Abstract template method that reads the actualy object. Invoked from AbstractHttpMessageConverter.read(java.lang.Class, org.springframework.http.HttpInputMessage).

Specified by:
readInternal in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the type of object to return
inputMessage - the HTTP input message to read from
Returns:
the converted object
Throws:
java.io.IOException - in case of I/O errors
HttpMessageNotReadableException - in case of conversion errors

writeInternal

protected void writeInternal(java.lang.Object object,
                             HttpOutputMessage outputMessage)
                      throws java.io.IOException,
                             HttpMessageNotWritableException
Description copied from class: AbstractHttpMessageConverter
Abstract template method that writes the actual body. Invoked from AbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).

Specified by:
writeInternal in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
object - the object to write to the output message
outputMessage - the message to write to
Throws:
java.io.IOException - in case of I/O errors
HttpMessageNotWritableException - in case of conversion errors

getJavaType

protected com.fasterxml.jackson.databind.JavaType getJavaType(java.lang.Class<?> clazz)
Return the Jackson 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);
   }
 }
 

Parameters:
clazz - the class to return the java type for
Returns:
the java type

getJsonEncoding

protected com.fasterxml.jackson.core.JsonEncoding getJsonEncoding(MediaType contentType)
Determine the JSON encoding to use for the given content type.

Parameters:
contentType - the media type as requested by the caller
Returns:
the JSON encoding to use (never null)

Spring Framework