Spring MVC View that renders JSON content by serializing the model for the current request
using Jackson 2'sObjectMapper.

By default, the entire contents of the model map (with the exception of framework-specific classes)
will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON
alone via setExtractValueFromSingleKeyModel(boolean).

Method Detail

setObjectMapper

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

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.

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:

The first step will be preparing the request: In the JSP case,
this would mean setting model objects as request attributes.
The second step will be the actual rendering of the view,
for example including the JSP via a RequestDispatcher.