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):

  1. JsonMessageConverter:顾名思义,它支持在 contentTypeapplication/json(默认)的情况下,将 Message 的有效负载转换为 POJO,反之亦然。

  2. JsonMessageConverter: As the name suggests it supports conversion of the payload of the Message to/from POJO for cases when contentType is application/json (DEFAULT).

  3. ByteArrayMessageConverter:支持在 contentTypeapplication/octet-stream 的情况下,将 Message 的有效内容从 byte[] 转换到 byte[]。它实质上是透传,并且主要为向后兼容而存在。

  4. ByteArrayMessageConverter: Supports conversion of the payload of the Message from byte[] to byte[] for cases when contentType is application/octet-stream. It is essentially a pass through and exists primarily for backward compatibility.

  5. ObjectStringMessageConverter:当 contentTypetext/plain 时支持将任何类型转换为 String 。它调用对象的 toString() 方法,或者在有效负载为 byte[] 的情况下,调用一个新 String(byte[])

  6. ObjectStringMessageConverter: Supports conversion of any type to a String when contentType is text/plain. It invokes Object’s toString() method or, if the payload is byte[], a new String(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.