类 MappingJackson2MessageConverter
- java.lang.Object
- org.springframework.messaging.converter.AbstractMessageConverter
- org.springframework.messaging.converter.MappingJackson2MessageConverter
- 所有已实现的接口:
MessageConverter
,SmartMessageConverter
public class MappingJackson2MessageConverter extends AbstractMessageConverter
A Jackson 2 basedMessageConverter
implementation.It customizes Jackson's default properties with the following ones:
MapperFeature.DEFAULT_VIEW_INCLUSION
is disabledDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
is disabled
Compatible with Jackson 2.6 and higher, as of Spring 4.3.
- 从以下版本开始:
- 4.0
- 作者:
- Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
字段概要
从类继承的字段 org.springframework.messaging.converter.AbstractMessageConverter
logger
构造器概要
构造器 构造器 说明 MappingJackson2MessageConverter()
Construct aMappingJackson2MessageConverter
supporting theapplication/json
MIME type withUTF-8
character set.MappingJackson2MessageConverter(MimeType... supportedMimeTypes)
Construct aMappingJackson2MessageConverter
supporting one or more custom MIME types.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected boolean
canConvertFrom(Message<?> message, Class<?> targetClass)
protected boolean
canConvertTo(Object payload, MessageHeaders headers)
protected Object
convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint)
Convert the message payload from serialized form to an Object.protected Object
convertToInternal(Object payload, MessageHeaders headers, Object conversionHint)
Convert the payload object to serialized form.protected JsonEncoding
getJsonEncoding(MimeType contentType)
Determine the JSON encoding to use for the given content type.ObjectMapper
getObjectMapper()
Return the underlyingObjectMapper
for this converter.protected Class<?>
getSerializationView(Object conversionHint)
Determine a Jackson serialization view based on the given conversion hint.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
setObjectMapper(ObjectMapper objectMapper)
Set theObjectMapper
for this converter.void
setPrettyPrint(boolean prettyPrint)
Whether to use theDefaultPrettyPrinter
when writing JSON.protected boolean
supports(Class<?> clazz)
Whether the given class is supported by this converter.从类继承的方法 org.springframework.messaging.converter.AbstractMessageConverter
convertFromInternal, convertToInternal, fromMessage, fromMessage, getContentTypeResolver, getDefaultContentType, getMimeType, getSerializedPayloadClass, getSupportedMimeTypes, isStrictContentTypeMatch, setContentTypeResolver, setSerializedPayloadClass, setStrictContentTypeMatch, supportsMimeType, toMessage, toMessage
构造器详细资料
MappingJackson2MessageConverter
public MappingJackson2MessageConverter()
Construct aMappingJackson2MessageConverter
supporting theapplication/json
MIME type withUTF-8
character set.
MappingJackson2MessageConverter
public MappingJackson2MessageConverter(MimeType... supportedMimeTypes)
Construct aMappingJackson2MessageConverter
supporting one or more custom MIME types.- 参数:
supportedMimeTypes
- the supported MIME types- 从以下版本开始:
- 4.1.5
方法详细资料
setObjectMapper
public void setObjectMapper(ObjectMapper objectMapper)
Set theObjectMapper
for this converter. If not set, a defaultObjectMapper
is used.Setting a custom-configured
ObjectMapper
is one way to take further control of the JSON serialization process. For example, an extendedSerializerFactory
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.
getObjectMapper
public ObjectMapper getObjectMapper()
Return the underlyingObjectMapper
for this converter.
setPrettyPrint
public void setPrettyPrint(boolean prettyPrint)
Whether to use theDefaultPrettyPrinter
when writing JSON. This is a shortcut for setting up anObjectMapper
as follows:ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); converter.setObjectMapper(mapper);
canConvertFrom
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass)
canConvertTo
protected boolean canConvertTo(Object payload, MessageHeaders headers)
logWarningIfNecessary
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.- 参数:
type
- the class that Jackson tested for (de-)serializabilitycause
- the Jackson-thrown exception to evaluate (typically aJsonMappingException
)- 从以下版本开始:
- 4.3
supports
protected boolean supports(Class<?> clazz)
从类复制的说明:AbstractMessageConverter
Whether the given class is supported by this converter.- 指定者:
supports
在类中AbstractMessageConverter
- 参数:
clazz
- the class to test for support- 返回:
true
if supported;false
otherwise
convertFromInternal
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint)
从类复制的说明:AbstractMessageConverter
Convert the message payload from serialized form to an Object.- 覆盖:
convertFromInternal
在类中AbstractMessageConverter
- 参数:
message
- the input messagetargetClass
- the target class for the conversionconversionHint
- an extra object passed to theMessageConverter
, e.g. the associatedMethodParameter
(may benull
}- 返回:
- the result of the conversion, or
null
if the converter cannot perform the conversion
convertToInternal
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint)
从类复制的说明:AbstractMessageConverter
Convert the payload object to serialized form.- 覆盖:
convertToInternal
在类中AbstractMessageConverter
- 参数:
payload
- the Object to convertheaders
- optional headers for the message (may benull
)conversionHint
- an extra object passed to theMessageConverter
, e.g. the associatedMethodParameter
(may benull
}- 返回:
- the resulting payload for the message, or
null
if the converter cannot perform the conversion
getSerializationView
protected Class<?> getSerializationView(Object conversionHint)
Determine a Jackson serialization view based on the given conversion hint.- 参数:
conversionHint
- the conversion hint Object as passed into the converter for the current conversion attempt- 返回:
- the serialization view class, or
null
if none - 从以下版本开始:
- 4.2
getJsonEncoding
protected JsonEncoding getJsonEncoding(MimeType contentType)
Determine the JSON encoding to use for the given content type.- 参数:
contentType
- the MIME type from the MessageHeaders, if any- 返回:
- the JSON encoding to use (never
null
)