Quick revision
The short answer
We declare a @Bean Function<OrderPlaced, InvoiceRequested> invoice() that maps each order to an invoice request, set spring.cloud.function.definition=invoice, and point the bindings invoice-in-0 and invoice-out-0 at the orders and invoices destinations with a consumer group. For the audit event, which starts in a REST call rather than in a message, the controller uses StreamBridge.send("audit-out-0", event), with that binding's destination set in properties. Payloads are converted to and from JSON by default, and the old @EnableBinding and @StreamListener model must not be used, because it was deprecated in 3.1 and removed in 4.0.
What to remember
In this answer, we wrote a Spring Cloud Stream service in the functional model: a Function<OrderPlaced, InvoiceRequested> bean bound through invoice-in-0 and invoice-out-0 to the orders and invoices destinations with a consumer group, and a REST controller that sends AuditEvent with StreamBridge through a configured audit-out-0 binding. JSON conversion is the default, a test binder lets us check it without a broker, and the annotation model is gone since 4.0.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We declare a @Bean Function<OrderPlaced, InvoiceRequested> invoice() that maps each order to an invoice request, set spring.cloud.function.definition=invoice, and point the bindings invoice-in-0 and invoice-out-0 at the orders and invoices destinations with a consumer group. For the audit event, which starts in a REST call rather than in a message, the controller uses StreamBridge.send("audit-out-0", event), with that binding's destination set in properties. Payloads are converted to and from JSON by default, and the old @EnableBinding and @StreamListener model must not be used, because it was deprecated in 3.1 and removed in 4.0.
Background to this question
This is the shape of most event-driven services: something arrives on a topic, the service reacts and publishes a new event, and occasionally an event starts from a user's HTTP request instead. Spring Cloud Stream's functional model covers both with very little code, but the property names must match the bean names exactly, and that is where people slip. In this answer, we will recap the functional model, write the events and the function, add the imperative send from the controller, put together the full set of properties, and finish with a test and a note on the old annotation model.
1A Quick Recap of the Functional Model
In Spring Cloud Stream, a message handler is an ordinary bean of a java.util.function type. A Supplier produces messages, a Function receives one and returns one, and a Consumer receives one and returns nothing. For each bean, the framework creates bindings, named connections to broker destinations, called beanName-in-0 for the input and beanName-out-0 for the output. Properties then map each binding to a real topic or exchange.
That model works when messages come in. When the trigger is something else, such as an HTTP request, there is no incoming message to return a result from, so we need a way to send imperatively. That is StreamBridge.
4 messages in order. Play them, or step through them one at a time.
The function is called by the framework; StreamBridge is called by our code.
2The Events and the Function
We model the events as records, which serialise cleanly to JSON.
Notice that the function knows nothing about Kafka, RabbitMQ or messages. The framework reads the payload, converts it from JSON to OrderPlaced because the default content type is application/json, calls our function, converts the result back to JSON and publishes it to the output binding. The broker is chosen by the binder dependency we add, such as spring-cloud-stream-binder-kafka.
3Sending the AuditEvent From a REST Controller
The audit event is created when an administrator cancels an order over HTTP. StreamBridge is a bean the framework provides for exactly this. We give it a binding name and a payload, and it converts and publishes the payload through that output binding, creating the binding on first use if needed.
Using a binding name such as audit-out-0, rather than a raw destination name, lets us configure the destination and producer settings in properties like any other binding. StreamBridge also accepts a destination name directly, in which case the destination is used as given; check how your version resolves names that match no configured binding. We can declare the binding up front with spring.cloud.stream.output-bindings=audit-out-0, so it exists at startup and its health is visible before the first send.
4The Properties
Now the configuration that ties it together.
Each line has a reason. The function definition tells the framework which bean to bind; it is optional with a single functional bean, but needed as soon as there are several, and it keeps the configuration explicit. The destinations replace the default, which would be the binding name itself. The group makes instances share the work and keeps a durable subscription, so a restarted instance resumes from the group's offsets. Notice that StreamBridge's binding is not listed in the function definition: only beans that the framework should call go there.
5Testing It Without a Broker
Spring Cloud Stream ships a test binder that replaces the real broker in tests. We send a message to the input destination and read what came out of the output destination.
Because the function is plain Java, we can also unit-test it by calling invoice().apply(...) directly, which needs no Spring context at all.
6A Note on the Old Annotation Model
Older examples use @EnableBinding with interfaces such as Processor, and @StreamListener and @SendTo on handler methods. That model was deprecated in Spring Cloud Stream 3.1 and removed in 4.0, which is the line used with Spring Boot 3. New code should use only the functional model and StreamBridge, and an interviewer who sees @StreamListener in a solution today will treat it as out of date.
7Follow-up questions
Why not inject a MessageChannel or KafkaTemplate in the controller?
KafkaTemplate would tie the controller to Kafka and bypass the binding configuration, so content type, destination and producer settings would be configured in two places. StreamBridge uses the same binder and conversion as the function, so all messaging stays consistent.
What happens if a function returns null?
No output message is sent for that input. It is a simple way to drop messages, such as orders with a zero total, without throwing. If a function never produces output, a Consumer bean is clearer.
How would I send the audit event with a Kafka key?
We send a Message instead of a bare payload, built with MessageBuilder.withPayload(event).setHeader(KafkaHeaders.KEY, orderId). The Kafka binder uses that header as the record key, so all events for one order go to the same partition. Alternatively, a partition key expression can be set on the binding's producer properties.
8Conclusion
In this answer, we wrote a Spring Cloud Stream service in the functional model: a Function<OrderPlaced, InvoiceRequested> bean bound through invoice-in-0 and invoice-out-0 to the orders and invoices destinations with a consumer group, and a REST controller that sends AuditEvent with StreamBridge through a configured audit-out-0 binding. JSON conversion is the default, a test binder lets us check it without a broker, and the annotation model is gone since 4.0.
Open with
I'd write it in the functional model. The events are records, OrderPlaced, InvoiceRequested and AuditEvent. The processing is a bean, Function of OrderPlaced to InvoiceRequested, called invoice, that just maps the fields.
Then, if they want more
Spring Cloud Stream creates two bindings for it, invoice-in-0 and invoice-out-0, converts the payloads from and to JSON by default, and the binder dependency, say Kafka, does the broker work. For the audit event, there's no incoming message, so in the controller I inject StreamBridge and call send with the binding name audit-out-0 and the event.
The properties are: spring.cloud.function.definition equals invoice, which is required once there's more than one functional bean and good practice anyway; invoice-in-0.destination equals orders with group billing, so instances share partitions and resume from committed offsets; invoice-out-0.destination equals invoices; and audit-out-0.destination equals audit, optionally declared with spring.cloud.stream.output-bindings so it exists at startup. Plus the binder's broker address.
I'd test with the test binder, sending to the orders input destination and reading from invoices, and unit-test the function directly. And I wouldn't use EnableBinding or StreamListener: that annotation model was deprecated in 3.1 and removed in 4.0.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Cloud Stream reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a Spring Cloud Stream functional application that reads OrderPlaced events, emits...": https://scrivio-interview-library.netlify.app/spring-boot/spring-cloud-stream/eb351dee72/