类 MappingJackson2MessageConverter
- java.lang.Object
- org.springframework.messaging.converter.AbstractMessageConverter
- org.springframework.messaging.converter.MappingJackson2MessageConverter
- 所有已实现的接口:
MessageConverter,SmartMessageConverter
public class MappingJackson2MessageConverter extends AbstractMessageConverter
A Jackson 2 basedMessageConverterimplementation.It customizes Jackson's default properties with the following ones:
MapperFeature.DEFAULT_VIEW_INCLUSIONis disabledDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESis disabled
Compatible with Jackson 2.9 and higher, as of Spring 5.1.
- 从以下版本开始:
- 4.0
- 作者:
- Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
字段概要
从类继承的字段 org.springframework.messaging.converter.AbstractMessageConverter
logger
构造器概要
构造器 构造器 说明 MappingJackson2MessageConverter()Construct aMappingJackson2MessageConvertersupporting theapplication/jsonMIME type withUTF-8character set.MappingJackson2MessageConverter(MimeType... supportedMimeTypes)Construct aMappingJackson2MessageConvertersupporting one or more custom MIME types.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected booleancanConvertFrom(Message<?> message, Class<?> targetClass)protected booleancanConvertTo(Object payload, MessageHeaders headers)protected ObjectconvertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint)Convert the message payload from serialized form to an Object.protected ObjectconvertToInternal(Object payload, MessageHeaders headers, Object conversionHint)Convert the payload object to serialized form.protected JsonEncodinggetJsonEncoding(MimeType contentType)Determine the JSON encoding to use for the given content type.ObjectMappergetObjectMapper()Return the underlyingObjectMapperfor this converter.protected Class<?>getSerializationView(Object conversionHint)Determine a Jackson serialization view based on the given conversion hint.protected voidlogWarningIfNecessary(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.voidsetObjectMapper(ObjectMapper objectMapper)Set theObjectMapperfor this converter.voidsetPrettyPrint(boolean prettyPrint)Whether to use theDefaultPrettyPrinterwhen writing JSON.protected booleansupports(Class<?> clazz)Whether the given class is supported by this converter.从类继承的方法 org.springframework.messaging.converter.AbstractMessageConverter
addSupportedMimeTypes, fromMessage, fromMessage, getContentTypeResolver, getDefaultContentType, getMimeType, getSerializedPayloadClass, getSupportedMimeTypes, isStrictContentTypeMatch, setContentTypeResolver, setSerializedPayloadClass, setStrictContentTypeMatch, supportsMimeType, toMessage, toMessage
构造器详细资料
MappingJackson2MessageConverter
public MappingJackson2MessageConverter()
Construct aMappingJackson2MessageConvertersupporting theapplication/jsonMIME type withUTF-8character set.
MappingJackson2MessageConverter
public MappingJackson2MessageConverter(MimeType... supportedMimeTypes)
Construct aMappingJackson2MessageConvertersupporting one or more custom MIME types.- 参数:
supportedMimeTypes- the supported MIME types- 从以下版本开始:
- 4.1.5
方法详细资料
setObjectMapper
public void setObjectMapper(ObjectMapper objectMapper)
Set theObjectMapperfor this converter. If not set, a defaultObjectMapperis used.Setting a custom-configured
ObjectMapperis one way to take further control of the JSON serialization process. For example, an extendedSerializerFactorycan 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 underlyingObjectMapperfor this converter.
setPrettyPrint
public void setPrettyPrint(boolean prettyPrint)
Whether to use theDefaultPrettyPrinterwhen writing JSON. This is a shortcut for setting up anObjectMapperas follows:ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); converter.setObjectMapper(mapper);
canConvertFrom
protected boolean canConvertFrom(Message<?> message, @Nullable Class<?> targetClass)
canConvertTo
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers)
logWarningIfNecessary
protected void logWarningIfNecessary(Type type, @Nullable 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)
从类复制的说明:AbstractMessageConverterWhether the given class is supported by this converter.- 指定者:
supports在类中AbstractMessageConverter- 参数:
clazz- the class to test for support- 返回:
trueif supported;falseotherwise
convertFromInternal
@Nullable protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint)
从类复制的说明:AbstractMessageConverterConvert 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
nullif the converter cannot perform the conversion
convertToInternal
@Nullable protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint)
从类复制的说明:AbstractMessageConverterConvert 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
nullif the converter cannot perform the conversion
getSerializationView
@Nullable protected Class<?> getSerializationView(@Nullable 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
nullif none - 从以下版本开始:
- 4.2
getJsonEncoding
protected JsonEncoding getJsonEncoding(@Nullable 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)