Package org.springframework.http.codec.json

# Class Jackson2CodecSupport

java.lang.Object
org.springframework.http.codec.json.Jackson2CodecSupport

Direct Known Subclasses:
  `AbstractJackson2Decoder`, `AbstractJackson2Encoder`

public abstract class Jackson2CodecSupport extends Object
Base class providing support methods for Jackson 2.x encoding and decoding.

Since:
  5.0
Author:
  Sebastien Deleuze, Rossen Stoyanchev

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`JSON_VIEW_HINT`
The key for the hint to specify a "JSON View" for encoding or decoding with the value expected to be a `Class`.
`protected final org.apache.commons.logging.Log`
`logger`

## Constructor Summary

Constructors
Modifier
Constructor
Description
`protected`
`Jackson2CodecSupport(ObjectMapper objectMapper, org.springframework.util.MimeType... mimeTypes)`
Constructor with a Jackson `ObjectMapper` to use.

## Method Summary

Modifier and Type
Method
Description
`protected abstract <A extends Annotation>
A`
`getAnnotation(org.springframework.core.MethodParameter parameter, Class<A> annotType)`
`protected Map<String,Object>`
`getHints(org.springframework.core.ResolvableType resolvableType)`
`protected JavaType`
`getJavaType(Type type, Class<?> contextClass)`
`protected List<org.springframework.util.MimeType>`
`getMediaTypesForProblemDetail()`
Return the supported media type(s) for `ProblemDetail`.
`protected List<org.springframework.util.MimeType>`
`getMimeTypes()`
Subclasses should expose this as "decodable" or "encodable" mime types.
`protected List<org.springframework.util.MimeType>`
`getMimeTypes(org.springframework.core.ResolvableType elementType)`
`ObjectMapper`
`getObjectMapper()`
Return the `configured` default ObjectMapper.
`protected Map<Class<?>,Map<org.springframework.util.MimeType,ObjectMapper>>`
`getObjectMapperRegistrations()`
`Map<org.springframework.util.MimeType,ObjectMapper>`
`getObjectMappersForType(Class<?> clazz)`
Return ObjectMapper registrations for the given class, if any.
`protected org.springframework.core.MethodParameter`
`getParameter(org.springframework.core.ResolvableType type)`
`protected void`
`logWarningIfNecessary(Type type, Throwable cause)`
Determine whether to log the given exception coming from a `ObjectMapper.canDeserialize(com.fasterxml.jackson.databind.JavaType)` / `ObjectMapper.canSerialize(java.lang.Class<?>)` check.
`void`
`registerObjectMappersForType(Class<?> clazz, Consumer<Map<org.springframework.util.MimeType,ObjectMapper>> registrar)`
Configure the `ObjectMapper` instances to use for the given `Class`.
`protected ObjectMapper`
`selectObjectMapper(org.springframework.core.ResolvableType targetType, org.springframework.util.MimeType targetMimeType)`
Select an ObjectMapper to use, either the main ObjectMapper or another if the handling for the given Class has been customized through `registerObjectMappersForType(Class, Consumer)`.
`void`
`setObjectMapper(ObjectMapper objectMapper)`
Configure the default ObjectMapper instance to use.
`protected boolean`
`supportsMimeType(org.springframework.util.MimeType mimeType)`

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

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

## Field Details

### JSON_VIEW_HINT

public static final String JSON_VIEW_HINT
The key for the hint to specify a "JSON View" for encoding or decoding with the value expected to be a `Class`.

See Also:
  - Jackson JSON Views

### logger

protected final org.apache.commons.logging.Log logger

## Constructor Details

### Jackson2CodecSupport

protected Jackson2CodecSupport(ObjectMapper objectMapper, org.springframework.util.MimeType... mimeTypes)
Constructor with a Jackson `ObjectMapper` to use.

## Method Details

### setObjectMapper

public void setObjectMapper(ObjectMapper objectMapper)
Configure the default ObjectMapper instance to use.

Parameters:
  `objectMapper` - the ObjectMapper instance
Since:
  5.3.4

### getObjectMapper

public ObjectMapper getObjectMapper()
Return the `configured` default ObjectMapper.

### registerObjectMappersForType

public void registerObjectMappersForType(Class<?> clazz, Consumer<Map<org.springframework.util.MimeType,ObjectMapper>> registrar)
Configure the `ObjectMapper` instances to use for the given `Class`. This is useful when you want to deviate from the `default` ObjectMapper or have the `ObjectMapper` vary by `MediaType`.

Note: Use of this method effectively turns off use of the default `ObjectMapper` and supported `MimeTypes` for the given class. Therefore it is important for the mappings configured here to `include` every MediaType that must be supported for the given class.

Parameters:
  `clazz` - the type of Object to register ObjectMapper instances for
  `registrar` - a consumer to populate or otherwise update the MediaType-to-ObjectMapper associations for the given Class
Since:
  5.3.4

### getObjectMappersForType

@Nullable public Map<org.springframework.util.MimeType,ObjectMapper> getObjectMappersForType(Class<?> clazz)
Return ObjectMapper registrations for the given class, if any.

Parameters:
  `clazz` - the class to look up for registrations for
Returns:
  a map with registered MediaType-to-ObjectMapper registrations, or empty if in case of no registrations for the given class.
Since:
  5.3.4

### getObjectMapperRegistrations

protected Map<Class<?>,Map<org.springframework.util.MimeType,ObjectMapper>> getObjectMapperRegistrations()

### getMimeTypes

protected List<org.springframework.util.MimeType> getMimeTypes()
Subclasses should expose this as "decodable" or "encodable" mime types.

### getMimeTypes

protected List<org.springframework.util.MimeType> getMimeTypes(org.springframework.core.ResolvableType elementType)

### getMediaTypesForProblemDetail

protected List<org.springframework.util.MimeType> getMediaTypesForProblemDetail()
Return the supported media type(s) for `ProblemDetail`. By default, an empty list, unless overridden in subclasses.

Since:
  6.0.5

### supportsMimeType

protected boolean supportsMimeType(@Nullable org.springframework.util.MimeType mimeType)

### logWarningIfNecessary

protected void logWarningIfNecessary(Type type, @Nullable Throwable cause)
Determine whether to log the given exception coming from a `ObjectMapper.canDeserialize(com.fasterxml.jackson.databind.JavaType)` / `ObjectMapper.canSerialize(java.lang.Class<?>)` check.

Parameters:
  `type` - the class that Jackson tested for (de-)serializability
  `cause` - the Jackson-thrown exception to evaluate (typically a `JsonMappingException`)
Since:
  5.3.1

### getJavaType

protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass)

### getHints

protected Map<String,Object> getHints(org.springframework.core.ResolvableType resolvableType)

### getParameter

@Nullable protected org.springframework.core.MethodParameter getParameter(org.springframework.core.ResolvableType type)

### getAnnotation

@Nullable protected abstract <A extends Annotation> A getAnnotation(org.springframework.core.MethodParameter parameter, Class<A> annotType)

### selectObjectMapper

@Nullable protected ObjectMapper selectObjectMapper(org.springframework.core.ResolvableType targetType, @Nullable org.springframework.util.MimeType targetMimeType)
Select an ObjectMapper to use, either the main ObjectMapper or another if the handling for the given Class has been customized through `registerObjectMappersForType(Class, Consumer)`.

Since:
  5.3.4