JasonS JasonS - 8 days ago 5
Java Question

Spring Cloud Stream: JSON array cannot be cast to POJO list

I'm doing some tests of the content type auto-conversion of Spring Cloud Stream.

As the spring-cloud-stream Guide shows. The json format of

GreetingMessage
(
{"greeting":"Hello, world"}
) will be cast to POJO
GreetingMessage
object automatically when consumes in the
@StreamListener
. This tests successfully.

But, when i change the output as
List<GreetingMessage > [{"greeting":"Hello, world"}, {"greeting":"Good morning"}]
, the input WILL NOT cast it to POJO
List<GreetingMessage >
objects, but as POJO
List<String>
.




So my question is:


  • Is this an existing issue or am i doing something wrong?






Code:

@Data
@AllArgsConstructor
public class GreetingMessage {
private String greeting;
}

@EnableBinding(Source.class)
public class GreetingSource {

@Bean
@InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public MessageSource<List<GreetingMessage>> greeting() {
return new MessageSource<List<GreetingMessage>>() {
public Message<List<GreetingMessage>> receive() {
return new GenericMessage(Arrays.asList(new GreetingMessage("hello"), new GreetingMessage("hello2")));
}
};
}
}

@EnableBinding(Sink.class)
public class GreetingSink {
@StreamListener(Sink.INPUT)
public void receive(Message<List<GreetingMessage>> msg) {
// handle GreetingMessage
System.out.println(msg);
}
}

Answer

This is a limitation of the message conversion process, where the parameterized type is not considered (just the raw List type). Please open an issue GitHub if you think this needs to be supported.