Reply ContentType
如果你正在使用成熟的消息转换器,如`ContentTypeDelegatingMessageConverter`,你可以通过设置侦听器的`replyContentType`属性来控制回复的内容类型。这允许转换器为回复选择合适的代理转换器。
If you are using a sophisticated message converter, such as the ContentTypeDelegatingMessageConverter
, you can control the content type of the reply by setting the replyContentType
property on the listener.
This allows the converter to select the appropriate delegate converter for the reply.
@RabbitListener(queues = "q1", messageConverter = "delegating",
replyContentType = "application/json")
public Thing2 listen(Thing1 in) {
...
}
默认情况下,为了向后兼容,转换器设置的任何内容类型属性都会在转换后被此值覆盖。SimpleMessageConverter`等转换器使用回复类型而不是内容类型来确定所需的转换,并相应地在回复消息中设置内容类型。这可能不是期望的动作,可以通过将`converterWinsContentType`属性设置为`false`来覆盖它。例如,如果你返回包含 JSON 的`String
,SimpleMessageConverter`将在回复中将内容类型设置为`text/plain
。即使使用了`SimpleMessageConverter`,以下配置也将确保内容类型设置正确。
By default, for backwards compatibility, any content type property set by the converter will be overwritten by this value after conversion.
Converters such as the SimpleMessageConverter
use the reply type rather than the content type to determine the conversion needed and sets the content type in the reply message appropriately.
This may not be the desired action and can be overridden by setting the converterWinsContentType
property to false
.
For example, if you return a String
containing JSON, the SimpleMessageConverter
will set the content type in the reply to text/plain
.
The following configuration will ensure the content type is set properly, even if the SimpleMessageConverter
is used.
@RabbitListener(queues = "q1", replyContentType = "application/json",
converterWinsContentType = "false")
public String listen(Thing in) {
...
return someJsonString;
}
当返回类型为 Spring AMQP Message`或 Spring Messaging `Message<?>`时,这些属性(`replyContentType`和`converterWinsContentType
)不适用。在第一种情况下,没有涉及转换;只需设置`contentType`消息属性即可。在第二种情况下,使用消息头控制行为:
These properties (replyContentType
and converterWinsContentType
) do not apply when the return type is a Spring AMQP Message
or a Spring Messaging Message<?>
.
In the first case, there is no conversion involved; simply set the contentType
message property.
In the second case, the behavior is controlled using message headers:
@RabbitListener(queues = "q1", messageConverter = "delegating")
@SendTo("q2")
public Message<String> listen(String in) {
...
return MessageBuilder.withPayload(in.toUpperCase())
.setHeader(MessageHeaders.CONTENT_TYPE, "application/xml")
.build();
}
此内容类型将作为`MessageProperties`传递给转换器。默认情况下,为了向后兼容,转换器设置的任何内容类型属性都会在转换后被此值覆盖。如果你希望覆盖该行为,还要将`AmqpHeaders.CONTENT_TYPE_CONVERTER_WINS`设置为`true`,并且转换器设置的任何值都将被保留。
This content type will be passed in the MessageProperties
to the converter.
By default, for backwards compatibility, any content type property set by the converter will be overwritten by this value after conversion.
If you wish to override that behavior, also set the AmqpHeaders.CONTENT_TYPE_CONVERTER_WINS
to true
and any value set by the converter will be retained.