public class

MappingJacksonHttpMessageConverter

extends AbstractHttpMessageConverter<T>
java.lang.Object
   ↳ org.springframework.http.converter.AbstractHttpMessageConverter<T>
     ↳ org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

Class Overview

Implementation of HttpMessageConverter that can read and write JSON using Jackson'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.

Summary

Fields
public static final Charset DEFAULT_CHARSET
[Expand]
Inherited Fields
From class org.springframework.http.converter.AbstractHttpMessageConverter
Public Constructors
MappingJacksonHttpMessageConverter()
Construct a new BindingJacksonHttpMessageConverter.
Public Methods
boolean canRead(Class<?> clazz, MediaType mediaType)
Indicates whether the given class can be read by this converter.

This implementation checks if the given class is supported, and if the supported media types include the given media type.

boolean canWrite(Class<?> clazz, MediaType mediaType)
Indicates whether the given class can be written by this converter.

This implementation checks if the given class is supported, and if the supported media types include the given media type.

void setObjectMapper(ObjectMapper objectMapper)
Sets the ObjectMapper for this view.
void setPrefixJson(boolean prefixJson)
Indicates whether the JSON output by this view should be prefixed with "{} &&".
Protected Methods
JavaType getJavaType(Class<?> clazz)
Returns the Jackson JavaType for the specific class.
Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
Abstract template method that reads the actualy object.
boolean supports(Class<?> clazz)
Indicates whether the given class is supported by this converter.
void writeInternal(Object o, HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.
[Expand]
Inherited Methods
From class org.springframework.http.converter.AbstractHttpMessageConverter
From class java.lang.Object
From interface org.springframework.http.converter.HttpMessageConverter

Fields

public static final Charset DEFAULT_CHARSET

Public Constructors

public MappingJacksonHttpMessageConverter ()

Construct a new BindingJacksonHttpMessageConverter.

Public Methods

public boolean canRead (Class<?> clazz, MediaType mediaType)

Indicates whether the given class can be read by this converter.

This implementation checks if the given class is supported, and if the supported media types include the given media type.

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

public boolean canWrite (Class<?> clazz, MediaType mediaType)

Indicates whether the given class can be written by this converter.

This implementation checks if the given class is supported, and if the supported media types include the given media type.

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

public void setObjectMapper (ObjectMapper objectMapper)

Sets the ObjectMapper for this view. If not set, a default ObjectMapper#ObjectMapper() ObjectMapper is used.

Setting a custom-configured ObjectMapper is one way to take further control of the JSON serialization process. For example, an extended org.codehaus.jackson.map.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 void setPrefixJson (boolean prefixJson)

Indicates 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.

Protected Methods

protected JavaType getJavaType (Class<?> clazz)

Returns the Jackson JavaType for the specific class.

Default implementation returns TypeFactory#type(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 TypeFactory.collectionType(ArrayList.class, MyBean.class);
   } else {
     return super.getJavaType(clazz);
   }
 }
 

Parameters
clazz the class to return the java type for
Returns
  • the java type

protected Object readInternal (Class<?> clazz, HttpInputMessage inputMessage)

Abstract template method that reads the actualy object. Invoked from read(Class, HttpInputMessage).

Parameters
clazz the type of object to return
inputMessage the HTTP input message to read from
Returns
  • the converted object

protected boolean supports (Class<?> clazz)

Indicates whether the given class is supported by this converter.

Parameters
clazz the class to test for support
Returns
  • true if supported; false otherwise

protected void writeInternal (Object o, HttpOutputMessage outputMessage)

Abstract template method that writes the actual body. Invoked from write(T, MediaType, HttpOutputMessage).

Parameters
o the object to write to the output message
outputMessage the message to write to