org.springframework.http.converter.json
Class MappingJacksonHttpMessageConverter<T>

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

public class MappingJacksonHttpMessageConverter<T>
extends AbstractHttpMessageConverter<T>

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, and overriding the getContentType(Object) method.

Since:
3.0
Author:
Arjen Poutsma
See Also:
org.springframework.web.servlet.view.json.BindingJacksonJsonView

Field Summary
 
Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
 
Constructor Summary
MappingJacksonHttpMessageConverter()
          Construct a new BindingJacksonHttpMessageConverter,
 
Method Summary
protected  MediaType getContentType(T t)
          Returns the content type for the given type.
protected  T readInternal(Class<T> clazz, HttpInputMessage inputMessage)
          Abstract template method that reads the actualy object.
 void setEncoding(org.codehaus.jackson.JsonEncoding encoding)
          Sets the JsonEncoding for this converter.
 void setObjectMapper(org.codehaus.jackson.map.ObjectMapper objectMapper)
          Sets the ObjectMapper for this view.
 void setPrefixJson(boolean prefixJson)
          Indicates whether the JSON output by this view should be prefixed with "{} &&".
 boolean supports(Class<? extends T> clazz)
          Indicate whether the given class is supported by this converter.
protected  void writeInternal(T t, HttpOutputMessage outputMessage)
          Abstract template method that writes the actual body.
 
Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
getContentLength, getSupportedMediaTypes, read, setSupportedMediaTypes, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MappingJacksonHttpMessageConverter

public MappingJacksonHttpMessageConverter()
Construct a new BindingJacksonHttpMessageConverter,

Method Detail

setObjectMapper

public void setObjectMapper(org.codehaus.jackson.map.ObjectMapper objectMapper)
Sets 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.


setEncoding

public void setEncoding(org.codehaus.jackson.JsonEncoding encoding)
Sets the JsonEncoding for this converter. By default, UTF-8 is used.


setPrefixJson

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.


supports

public boolean supports(Class<? extends T> clazz)
Description copied from interface: HttpMessageConverter
Indicate whether the given class is supported by this converter.

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

readInternal

protected T readInternal(Class<T> clazz,
                         HttpInputMessage inputMessage)
                  throws IOException,
                         HttpMessageNotReadableException
Description copied from class: AbstractHttpMessageConverter
Abstract template method that reads the actualy object. Invoked from AbstractHttpMessageConverter.read(Class, HttpInputMessage).

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

getContentType

protected MediaType getContentType(T t)
Description copied from class: AbstractHttpMessageConverter
Returns the content type for the given type.

By default, this returns the first element of the supportedMediaTypes property, if any. Can be overriden in subclasses.

Overrides:
getContentType in class AbstractHttpMessageConverter<T>
Parameters:
t - the type to return the content type for
Returns:
the content type, or null if not known

writeInternal

protected void writeInternal(T t,
                             HttpOutputMessage outputMessage)
                      throws IOException,
                             HttpMessageNotWritableException
Description copied from class: AbstractHttpMessageConverter
Abstract template method that writes the actual body. Invoked from AbstractHttpMessageConverter.write(Object, HttpOutputMessage).

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