Quick revision
The short answer
Spring Cloud Function lets us write business logic as plain java.util.function beans, Function, Supplier or Consumer, with no framework types, and then binds them to a runtime through adapters: HTTP with spring-cloud-function-web, messaging with Spring Cloud Stream, or serverless platforms such as AWS Lambda. With the web adapter, a Function<Order, Invoice> bean named invoice is exposed at POST /invoice; on Lambda, the AWS adapter's FunctionInvoker handler calls the function chosen by spring.cloud.function.definition. Functions compose with the pipe syntax, such as validate|invoice.
What to remember
In this answer, we saw that Spring Cloud Function keeps business logic in plain Function, Supplier and Consumer beans, registers them in a function catalog and binds them to runtimes through adapters. A Function<Order, Invoice> bean became POST /invoice with the web adapter and a Lambda handler with the AWS adapter's FunctionInvoker and spring.cloud.function.definition. We composed functions with validate|invoice and saw that message converters turn JSON into the declared types.
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.
Spring Cloud Function lets us write business logic as plain java.util.function beans, Function, Supplier or Consumer, with no framework types, and then binds them to a runtime through adapters: HTTP with spring-cloud-function-web, messaging with Spring Cloud Stream, or serverless platforms such as AWS Lambda. With the web adapter, a Function<Order, Invoice> bean named invoice is exposed at POST /invoice; on Lambda, the AWS adapter's FunctionInvoker handler calls the function chosen by spring.cloud.function.definition. Functions compose with the pipe syntax, such as validate|invoice.
Why this question is asked
Teams that write serverless code often end up with business logic tangled with a platform's request and context classes, which makes it hard to test and impossible to move. Spring Cloud Function takes the opposite approach: write the logic as ordinary Java functions and let Spring adapt them. Interviewers ask about it to see whether we understand the programming model and how the same bean reaches HTTP, a message broker or Lambda. In this answer, we will recap the idea, write a function, expose it over HTTP, deploy it on AWS Lambda, compose functions and see how type conversion works.
1A Quick Recap of the Idea
Java has three standard functional interfaces that cover most event handling. A Function<I, O> takes an input and returns an output, a Supplier<O> produces values with no input, and a Consumer<I> takes input and returns nothing. Spring Cloud Function treats any bean of these types as a function, registers it in a FunctionCatalog under its bean name, and wraps it so that it can be called with messages.
The adapters are the second half. Each one knows a runtime: how to receive a request or an event, turn it into input for the function, and turn the output back into a response. Our function knows nothing about them, which is what makes it portable and easy to unit test.
- Adaptersweb (HTTP), Spring Cloud Stream (Kafka, RabbitMQ), AWS, Azure
- Function cataloglooks up functions by name, composes, converts types
- Business functionsplain Function, Supplier, Consumer beans
- Domain codeservices and records, no platform types
4 layers, from the top down. Step through them one at a time.
2Writing the Function
The function below turns an order into an invoice. It is an ordinary bean; the bean name, invoice, becomes the function name.
Notice the test at the bottom. Because the function is plain Java, testing it is just calling apply.
3Exposing It Over HTTP
Adding the spring-cloud-starter-function-web dependency turns every function into an endpoint named after it. The web adapter reads the request body, converts it to Order, calls the function and writes the Invoice as JSON.
This is handy for local development and for running the same function in a container, but for a large public API we would still use controllers, which give finer control over status codes and headers.
4Running It on AWS Lambda
For Lambda we add the AWS adapter, spring-cloud-function-adapter-aws, package the application as Lambda expects, and set the handler to the adapter's generic invoker, org.springframework.cloud.function.adapter.aws.FunctionInvoker. When Lambda calls it, the invoker starts the Spring context once per execution environment, then for each event converts the payload, finds the function and calls it.
7 messages in order. Play them, or step through them one at a time.
If the application has exactly one function, the definition can be omitted, but setting it explicitly avoids surprises when someone adds a second bean. The same jar can then run on Lambda and, with the web adapter, locally.
5Composing Functions
Functions can be chained with the pipe symbol in the definition. If we add a validate function that checks an order and returns it, or throws, we can run both in one call.
The output type of each function must fit the input of the next, here Order to Order to Invoice. Composition keeps each function small and reusable, and lets us add steps such as enrichment by configuration rather than code.
6How Type Conversion Works
Inputs and outputs travel as messages with a byte payload and headers. Spring Cloud Function looks at the generic types of the function, Function<Order, Invoice>, and uses message converters, Jackson for JSON by default, to turn the payload into an Order and the Invoice back into bytes. The content type header decides which converter is used.
That is why the declared types matter. A Function<String, String> receives the raw text, while a Function<Order, Invoice> receives a ready object. If we need the headers too, we can declare Function<Message<Order>, Message<Invoice>> and read or set headers on the messages.
7Follow-up questions
How do you route to different functions based on a header?
Spring Cloud Function has a built-in RoutingFunction, registered as functionRouter. We set the definition to functionRouter and send the target function's name in the spring.cloud.function.definition message header, or configure spring.cloud.function.routing-expression with a SpEL expression such as headers['type'] == 'refund' ? 'refund' : 'invoice'. For more complex rules we can provide a MessageRoutingCallback bean. On Lambda the header can come from the event, so one deployment can serve several event types.
Is Spring Cloud Function only for serverless?
No. The same functions are used by Spring Cloud Stream to consume and produce messages on Kafka or RabbitMQ, which is the most common use in many companies.
What about cold starts on Lambda?
The Spring context starts on the first call of each execution environment, which can take seconds, so SnapStart, trimming dependencies or a native image are common remedies.
8Conclusion
In this answer, we saw that Spring Cloud Function keeps business logic in plain Function, Supplier and Consumer beans, registers them in a function catalog and binds them to runtimes through adapters. A Function<Order, Invoice> bean became POST /invoice with the web adapter and a Lambda handler with the AWS adapter's FunctionInvoker and spring.cloud.function.definition. We composed functions with validate|invoice and saw that message converters turn JSON into the declared types.
Open with
Spring Cloud Function is a programming model where business logic is written as plain Java functional beans, Function, Supplier or Consumer, without any framework or platform types. Spring registers them in a function catalog by bean name, and adapters bind them to a runtime.
Then, if they want more
For example, I declare a bean Function of Order to Invoice called invoice. With the spring-cloud-function-web starter, it becomes an HTTP endpoint at POST /invoice: the JSON body is converted to Order, the function runs, and the Invoice is written back as JSON. The same functions can be bound to Kafka or RabbitMQ through Spring Cloud Stream.
For AWS Lambda, I add the AWS adapter and set the handler to its generic FunctionInvoker; it starts Spring once per execution environment, and spring.cloud.function.definition selects which function to run.
Functions compose with a pipe in the definition, like validate|invoice, as long as the output of one matches the input of the next, so I can chain validation and calculation without writing glue code.
Type conversion uses message converters based on the declared generic types and the content type, so JSON maps straight to my records, and I can use Message types if I need headers. The big benefit is that the logic is trivially unit testable and portable between HTTP, messaging and serverless.
About this answer
- Last updated
- 12 November 2026
- 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 Boot 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 "What is Spring Cloud Function? Show how a java.util.function.Function bean becomes an...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/06c3bc7124/