Class Jackson2CodecSupport
java.lang.Object
org.springframework.http.codec.json.Jackson2CodecSupport
- Direct Known Subclasses:
AbstractJackson2Decoder
,AbstractJackson2Encoder
Base class providing support methods for Jackson 2.x encoding and decoding.
- Since:
- 5.0
- Author:
- Sebastien Deleuze, Rossen Stoyanchev
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
Jackson2CodecSupport
(ObjectMapper objectMapper, MimeType... mimeTypes) Constructor with a JacksonObjectMapper
to use. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract <A extends Annotation>
AgetAnnotation
(MethodParameter parameter, Class<A> annotType) getHints
(ResolvableType resolvableType) protected JavaType
getJavaType
(Type type, Class<?> contextClass) Return the supported media type(s) forProblemDetail
.Subclasses should expose this as "decodable" or "encodable" mime types.getMimeTypes
(ResolvableType elementType) Return theconfigured
default ObjectMapper.protected Map<Class<?>,
Map<MimeType, ObjectMapper>> getObjectMappersForType
(Class<?> clazz) Return ObjectMapper registrations for the given class, if any.protected MethodParameter
getParameter
(ResolvableType type) protected void
logWarningIfNecessary
(Type type, Throwable cause) Determine whether to log the given exception coming from aObjectMapper.canDeserialize(com.fasterxml.jackson.databind.JavaType)
/ObjectMapper.canSerialize(java.lang.Class<?>)
check.void
registerObjectMappersForType
(Class<?> clazz, Consumer<Map<MimeType, ObjectMapper>> registrar) Configure theObjectMapper
instances to use for the givenClass
.protected ObjectMapper
selectObjectMapper
(ResolvableType targetType, MimeType targetMimeType) Select an ObjectMapper to use, either the main ObjectMapper or another if the handling for the given Class has been customized throughregisterObjectMappersForType(Class, Consumer)
.void
setObjectMapper
(ObjectMapper objectMapper) Configure the default ObjectMapper instance to use.protected boolean
supportsMimeType
(MimeType mimeType)
-
Field Details
-
JSON_VIEW_HINT
The key for the hint to specify a "JSON View" for encoding or decoding with the value expected to be aClass
.- See Also:
-
logger
-
-
Constructor Details
-
Jackson2CodecSupport
Constructor with a JacksonObjectMapper
to use.
-
-
Method Details
-
setObjectMapper
Configure the default ObjectMapper instance to use.- Parameters:
objectMapper
- the ObjectMapper instance- Since:
- 5.3.4
-
getObjectMapper
Return theconfigured
default ObjectMapper. -
registerObjectMappersForType
public void registerObjectMappersForType(Class<?> clazz, Consumer<Map<MimeType, ObjectMapper>> registrar) Configure theObjectMapper
instances to use for the givenClass
. This is useful when you want to deviate from thedefault
ObjectMapper or have theObjectMapper
vary byMediaType
.Note: Use of this method effectively turns off use of the default
ObjectMapper
and supportedMimeTypes
for the given class. Therefore it is important for the mappings configured here toinclude
every MediaType that must be supported for the given class.- Parameters:
clazz
- the type of Object to register ObjectMapper instances forregistrar
- a consumer to populate or otherwise update the MediaType-to-ObjectMapper associations for the given Class- Since:
- 5.3.4
-
getObjectMappersForType
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
-
getMimeTypes
Subclasses should expose this as "decodable" or "encodable" mime types. -
getMimeTypes
-
getMediaTypesForProblemDetail
Return the supported media type(s) forProblemDetail
. By default, an empty list, unless overridden in subclasses.- Since:
- 6.0.5
-
supportsMimeType
-
logWarningIfNecessary
Determine whether to log the given exception coming from aObjectMapper.canDeserialize(com.fasterxml.jackson.databind.JavaType)
/ObjectMapper.canSerialize(java.lang.Class<?>)
check.- Parameters:
type
- the class that Jackson tested for (de-)serializabilitycause
- the Jackson-thrown exception to evaluate (typically aJsonMappingException
)- Since:
- 5.3.1
-
getJavaType
-
getHints
-
getParameter
-
getAnnotation
@Nullable protected abstract <A extends Annotation> A getAnnotation(MethodParameter parameter, Class<A> annotType) -
selectObjectMapper
@Nullable protected ObjectMapper selectObjectMapper(ResolvableType targetType, @Nullable MimeType targetMimeType) Select an ObjectMapper to use, either the main ObjectMapper or another if the handling for the given Class has been customized throughregisterObjectMappersForType(Class, Consumer)
.- Since:
- 5.3.4
-