Provided MessageConverters
如前所述,该框架已经提供了一堆 MessageConverter 来处理大多数常见用例。以下列表描述了提供的 MessageConverter,按优先级顺序(使用第一个可用的 MessageConverter):
As mentioned earlier, the framework already provides a stack of MessageConverters
to handle most common use cases.
The following list describes the provided MessageConverters
, in order of precedence (the first MessageConverter
that works is used):
-
JsonMessageConverter
: As the name suggests it supports conversion of the payload of theMessage
to/from POJO for cases whencontentType
isapplication/json
(DEFAULT). -
ByteArrayMessageConverter
: Supports conversion of the payload of theMessage
frombyte[]
tobyte[]
for cases whencontentType
isapplication/octet-stream
. It is essentially a pass through and exists primarily for backward compatibility. -
ObjectStringMessageConverter
: Supports conversion of any type to aString
whencontentType
istext/plain
. It invokes Object’stoString()
method or, if the payload isbyte[]
, a newString(byte[])
.
如果未找到合适的转换器,该框架将引发异常。当发生这种情况时,您应该检查您的代码和配置,确保您没有遗漏任何内容(即确保您使用绑定或标头提供了 contentType
)。但是,很有可能您发现了一些不常见的情况(如自定义 contentType
),而当前提供的 MessageConverters`堆栈不知道如何转换。如果是这种情况,您可以添加自定义 `MessageConverter
。请参阅 User-defined Message Converters。
When no appropriate converter is found, the framework throws an exception. When that happens, you should check your code and configuration and ensure you did not miss anything (that is, ensure that you provided a contentType
by using a binding or a header).
However, most likely, you found some uncommon case (such as a custom contentType
perhaps) and the current stack of provided MessageConverters
does not know how to convert. If that is the case, you can add custom MessageConverter
. See User-defined Message Converters.