Package org.springframework.http.converter.json

# Class MappingJackson2HttpMessageConverter

java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<T>
org.springframework.http.converter.AbstractGenericHttpMessageConverter<Object>
org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

All Implemented Interfaces:
  `GenericHttpMessageConverter<Object>`, `HttpMessageConverter<Object>`

public class MappingJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter
Implementation of `HttpMessageConverter` that can read and write JSON using Jackson 2.x's `ObjectMapper`.

This converter can be used to bind to typed beans, or untyped `HashMap` instances.

By default, this converter supports `application/json` and `application/*+json` with `UTF-8` character set. This can be overridden by setting the `supportedMediaTypes` property.

The default constructor uses the default configuration provided by `Jackson2ObjectMapperBuilder`.

Since:
  3.1.2
Author:
  Arjen Poutsma, Keith Donald, Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze

## Field Summary

### Fields inherited from class org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter

`defaultObjectMapper`

### Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter

`logger`

## Constructor Summary

Constructors
Constructor
Description
`MappingJackson2HttpMessageConverter()`
Construct a new `MappingJackson2HttpMessageConverter` using default configuration provided by `Jackson2ObjectMapperBuilder`.
`MappingJackson2HttpMessageConverter(ObjectMapper objectMapper)`
Construct a new `MappingJackson2HttpMessageConverter` with a custom `ObjectMapper`.

## Method Summary

Modifier and Type
Method
Description
`protected List<MediaType>`
`getMediaTypesForProblemDetail()`
Return the supported media type(s) for `ProblemDetail`.
`void`
`setJsonPrefix(String jsonPrefix)`
Specify a custom prefix to use for this view's JSON output.
`void`
`setPrefixJson(boolean prefixJson)`
Indicate whether the JSON output by this view should be prefixed with ")]}', ".
`protected void`
`writePrefix(JsonGenerator generator, Object object)`
Write a prefix before the main content.

### Methods inherited from class org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter

`canRead, canRead, canWrite, customizeReader, customizeWriter, getCharset, getContentLength, getDefaultContentType, getJavaType, getJsonEncoding, getObjectMapper, getObjectMappersForType, getSupportedMediaTypes, logWarningIfNecessary, read, readInternal, registerObjectMappersForType, setObjectMapper, setPrettyPrint, setSupportedMediaTypes, supportsRepeatableWrites, writeInternal, writeSuffix`

### Methods inherited from class org.springframework.http.converter.AbstractGenericHttpMessageConverter

`canWrite, supports, write, writeInternal`

### Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter

`addDefaultHeaders, canRead, canWrite, getDefaultCharset, getSupportedMediaTypes, read, setDefaultCharset, write`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.springframework.http.converter.HttpMessageConverter

`getSupportedMediaTypes, read, write`

## Constructor Details

### MappingJackson2HttpMessageConverter

public MappingJackson2HttpMessageConverter()
Construct a new `MappingJackson2HttpMessageConverter` using default configuration provided by `Jackson2ObjectMapperBuilder`.

### MappingJackson2HttpMessageConverter

public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper)
Construct a new `MappingJackson2HttpMessageConverter` with a custom `ObjectMapper`. You can use `Jackson2ObjectMapperBuilder` to build it easily.

See Also:
  - `Jackson2ObjectMapperBuilder.json()`

## Method Details

### setJsonPrefix

public void setJsonPrefix(String jsonPrefix)
Specify a custom prefix to use for this view's JSON output. Default is none.

See Also:
  - `setPrefixJson(boolean)`

### 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 should be stripped before parsing the string as JSON.

See Also:
  - `setJsonPrefix(java.lang.String)`

### getMediaTypesForProblemDetail

protected List<MediaType> getMediaTypesForProblemDetail()
Description copied from class: `AbstractJackson2HttpMessageConverter`
Return the supported media type(s) for `ProblemDetail`. By default, an empty list, unless overridden in subclasses.

Overrides:
  `getMediaTypesForProblemDetail` in class `AbstractJackson2HttpMessageConverter`

### writePrefix

protected void writePrefix(JsonGenerator generator, Object object) throws IOException
Description copied from class: `AbstractJackson2HttpMessageConverter`
Write a prefix before the main content.

Overrides:
  `writePrefix` in class `AbstractJackson2HttpMessageConverter`
Parameters:
  `generator` - the generator to use for writing content.
  `object` - the object to write to the output message.
Throws:
  `IOException`