0

我有以下代码:

@Service
@EnableBinding(Sink.class)
public class AMQPService {
    ObjectMapper mapper = new ObjectMapper();

    @Autowired
    private BinderAwareChannelResolver binderAwareChannelResolver;

    @StreamListener(Sink.INPUT)
    public void processMessage(@Payload Map<String, Object> inboundMessage, @Headers Map<String, Object> headers) throws JsonParseException, JsonMappingException, IOException {

        headers.entrySet().forEach(e -> System.out.println(e.getKey() + '=' + e.getValue()));

        String output = mapper.writeValueAsString(inboundMessage);
        AMQPOutboundMessage outMessage = new AMQPOutboundMessage();
        outMessage.setText(output);
        if (headers.containsKey("expected_destination")) {
            MessageChannel messageChannel = binderAwareChannelResolver.resolveDestination(headers.get("expected_destination").toString());
            messageChannel.send(MessageBuilder.withPayload(outMessage).setHeader("contentType", "application/json;charset=UTF-8").build());
    }
}

}

它只是从 RabbitMQ 获取 amqp 消息,然后根据“expected_destination”标头将消息发送到目的地。

我已经设置spring.cloud.stream.bindings.output.content-type=application/json;charset=UTF-8了,但我看到消息的内容类型是application/x-java-object;type=xxx.AMQPOutboundMessagebase64 编码的消息正文。

但是当我@Autowired用来获取messageChannel时,似乎一切都很好。

那么,我可以知道在这种情况下如何设置内容类型吗?

4

1 回答 1

0

该设置spring.cloud.stream.bindings.output.content-type=application/json;charset=UTF-8仅适用于名为 的通道output,而不适用于所有输出通道。

在这个版本中,您需要为每个预期输出进行配置,即expected_destination. 我们正在考虑将来支持默认设置,即https://github.com/spring-cloud/spring-cloud-stream/issues/446

于 2016-08-16T04:25:20.140 回答