我有以下代码:
@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.AMQPOutboundMessage
base64 编码的消息正文。
但是当我@Autowired
用来获取messageChannel时,似乎一切都很好。
那么,我可以知道在这种情况下如何设置内容类型吗?