我跟着我的上一個問題Spring Cloud Stream message from/to JSON conversion configuration和配置的流如上所述,但我無法使其正常工作。Spring雲流消息JSON轉換不起作用
我的設置如下。我有兩個應用程序A
和B
。應用A
使用輸入通道one
,輸出two
。 App B
使用輸入two
。頻道two
配置了內容類型application/json
。
App A. Properties。
spring.cloud.stream.bindings.input.destination=one
spring.cloud.stream.bindings.input.group=default
spring.cloud.stream.bindings.output.destination=two
spring.cloud.stream.bindings.output.content-type=application/json
監聽方法。
@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Dto handle(byte[] payload) throws IOException {
final Dto dto = new ObjectMapper().readValue(payload, Dto.class);
logger.info("{}", dto);
dto.setId(dto.getId() + 1000);
return dto;
}
App B. Properties。
spring.cloud.stream.bindings.input.destination=two
spring.cloud.stream.bindings.input.group=default
spring.cloud.stream.bindings.input.content-type=application/json
聽衆的方法。
@ServiceActivator(inputChannel = Sink.INPUT)
public void handle(Dto dto) throws IOException {
logger.info("DTO {}", dto);
}
當我手動發送的消息與適當的JSON字符串到通道one
,它被正確地處理併發送的信道two
作爲JSON消息(頭如在上面提到的問題中所述完全相同的)。在此之後,它是由應用程序B接收信道two
並拋出異常:Method handle(java.lang.String) cannot be found
當然,當我創建這兩種方法,處理DTO和字符串作爲輸入,它的工作原理,但總是字符串方法被調用,並有反序列化我自己的有效載荷。
我誤會了嗎?如何設置帶有這種簽名的方法:public Dto handle(Dto incoming)
?
爲什麼你不使用'StreamListener'而不是'ServiceActivator'作爲你的App B?我不是100%確定的,但我認爲你的問題可以通過'StreamListener'修復,而不是改變'content-type'屬性。 – Gooseman
當時,此類選項不可用。但是,是的,這是一個很好的建議。 – waste