Search code examples
spring-cloud-streamspring-cloud-functionspring-cloud-stream-binder-kinesis

Route based on payload in Spring Cloud Stream


Is it possible to use spring.cloud.function.routing-expression to route based on some field in the payload rather than headers? I have a use case where the pubisher most probably won't be sending headers.

From what I understand, payload conversion from byte[] to a specific type is not possible, however, how do you route if headers are not an option?


Solution

  • You can't and you should absolutely not. It is a very bad practice and quite frankly an anti-pattern that is unfortunately being applied throughout. .

    Just like a regular letter or a package, the payload is privileged information. You don't expect Post office or Fedex to open it to make some routing decisions. . and you should not expect the framework to to do so either. On top of that, what type would you expect the payload to be converted to? Imagine you have two functions. One take Foo and one takes Bar type as input argument. We can only make a type conversion decision when we know the function we will be invoking and to know that we need to finish with routing decision. There is also a section in the reference manual that talks more about it - https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#spel-and-streaming-data