阅读背景:

Spring Cloud Stream消息JSON转换无效

来源:互联网 

I followed my previous question Spring Cloud Stream message from/to JSON conversion configuration and configured stream as described, yet, I can't make it work correctly.

我按照上面提到的问题将Spring Cloud Stream消息从/到JSON转换配置和配置流按照描述,但是,我无法使其正常工作。

My setup is as follows. I have two apps A and B. App A uses input channel one, output two. App B uses input two. Channel two is configured with content type application/json.

我的设置如下。我有两个应用程序A和B.应用程序A使用输入通道一,输出两个。应用B使用输入二。通道2配置有内容类型application / json。

App A. Properties.

App A.属性。

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

Listener method.

@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.

应用B.属性。

spring.cloud.stream.bindings.input.destination=two
spring.cloud.stream.bindings.input.group=default
spring.cloud.stream.bindings.input.content-type=application/json

Listener method.

@ServiceActivator(inputChannel = Sink.INPUT)
public void handle(Dto dto) throws IOException {
    logger.info("DTO {}", dto);
}

When I manually send a message with proper JSON string to channel one, it is processed correctly and send to channel two as a JSON message (headers exactly the same as described in above mentioned question). After that, it's received on channel two by App B and exception is thrown: Method handle(java.lang.String) cannot be found

当我手动将带有正确JSON字符串的消息发送到通道1时,它会被正确处理并作为JSON消息发送到通道2(标题与上述问题中描述的完全相同)。之后,App B在第二频道收到它并抛出异常:找不到方法句柄(java.lang.String)

Of course when I create both methods, handling Dto and String as an input, it works, but always String method is invoked and have to deserialize the payload by myself.

当然,当我创建两个方法时,处理Dto和String作为输入,它可以工作,但总是调用String方法并且必须自己反序列化有效负载。

Am I mistaken somewhere? How do I setup method with such signature: public Dto handle(Dto incoming)?

我错了吗?如何设置具有此类签名的方法:public Dto handle(Dto incoming)?

1 个解决方案

#1


1  

You should change the content-type declaration of input of AppB to

您应该将AppB输入的内容类型声明更改为

application/x-java-object;type=your.package.Dto.

As it is specified in your question, of course you accept JSON strings only.

正如您在问题中指定的那样,您当然只接受JSON字符串。


分享到: