Spring Cloud Stream Data Type in Function

Options
kirthi
kirthi Member Posts: 18
edited April 2023 in General Discussions #1

 Function<Message<?>, Message<?>>

The function takes the input paylaod as bytes even when datatype is mentioned. The auto conversion from json to pojo is not happening.

Any suggestion?

For example, Message<ClassName> when i directly use it, i am getting Class class exception

ClassName a = message.getPayload().

 Works as only this : String messageBytes = new String((byte[]) payload, StandardCharsets.UTF_8);

Answers

  • marc
    marc Member, Administrator, Moderator, Employee Posts: 920 admin
    Options

    Hi @kirthi ,

    If you haven't already I would suggest reading through the Content Negotiation section of the Spring Cloud Stream reference guide. It does a good job at explaining all of the options, including Provided Message Converters and how you can create custom ones. If you are getting a class cast exception then there is an issue where it can't parse the inbound payload into the expected object. Does the exception give you any details that help? otherwise you might have to debug the code to see what it's failing on.

    Note that by default if your signature says Function<Message<Foo>,Message<Bar>> then on the input binding it will try to convert the object into a Foo POJO and on the outbound side it will expect a Bar object to be returned.

    Hope that helps.

  • kirthi
    kirthi Member Posts: 18
    Options

    Hi All,

    Please suggest for the below scenario;

    Function<Message<List<?>>, Collection<//User Defined Object//>> receiveMessage() 

    Will we be able to publish the payload as object instead of wrapping it in org.springframework.messaging.Message with messageBuilder?

    Will the next microservice can it receive as the same Object?

    Function<//User Defined Object//, //User Defined Object//>> messageReceiver();

     Exception Occured while testing :

    nested exception is com.solace.spring.cloud.stream.binder.util.SolaceMessageConversionException: Invalid payload received. Expected byte[], String, SDTStream, SDTMap, Serializable. 

    Should the message always be wrapped as message?..

  • kirthi
    kirthi Member Posts: 18
    Options

    Thanks @marc

    Producing application:

    spring.cloud.stream.bindings.receiveMessage-out-0.content-type=application/json

    spring.cloud.stream.bindings.receiveMessage-out-0.producer.content-type=application/json


    Receving application;

    spring.cloud.stream.bindings.messageReceiver-in-0.content-type=application/json

    spring.cloud.stream.bindings.messageReceiver-in-0.consumer.content-type=application/json

    Is this is the intended property?