9.1.2 FunctionBean
This section applies to Java & Kotlin functions - for functions written in Groovy, see Groovy Functions |
To write your function’s behavior, annotate your class with the @FunctionBean annotation. Your class must also implement one of the interfaces from the java.util.function
package.
If you have the Micronaut CLI installed you can quickly create a Java function with mn create-function hello-world or mn create-function hello-world —lang kotlin for Kotlin |
The following examples implement Java’s Supplier functional interface.
Example Java Function
package example;
import io.micronaut.function.FunctionBean;
import java.util.function.Supplier;
@FunctionBean("hello-world-java")
public class HelloJavaFunction implements Supplier<String> {
@Override
public String get() { (1)
return "Hello world!";
}
}
1 | Override the get method of Supplier to return the response from your function. |
Alternatively you can also define a Factory that returns a Java lambda:
Example Java Function as a Lambda
package example;
import io.micronaut.context.annotation.*;
import io.micronaut.function.FunctionBean;
import java.util.function.Supplier;
@Factory (1)
public class MyFunctions {
@FunctionBean("hello-world-java")
public Supplier<String> helloWorld() { (2)
return () -> "Hello world!";
}
}
1 | A Factory bean is defined |
2 | The @FunctionBean annotation is used on a method that returns the function. |
If you are using Kotlin then process is exactly the same:
Example Kotlin Function
package example
import io.micronaut.function.FunctionBean
import java.util.function.Supplier
@FunctionBean("hello-world-kotlin")
class HelloKotlinFunction : Supplier<String> {
override fun get(): String { (1)
return "Hello world!"
}
}
1 | Override the get method of Supplier to return the response from your function. |
The following table summarizes the supported interfaces:
Interface | Dependency |
---|---|
Accepts no arguments and returns a single result | |
Accepts a single argument and returns no result | |
Accepts two arguments and returns no result | |
Accepts a single argument and returns a single result | |
Accepts two arguments and returns a single result |
In addition, functions have an input and/or an output. The input is represented by the accepted argument and represents the body consumed by the function and the output is represented by the return value of the function. The input and the output should be either a Java primitive or simple type (int
, String
etc.) or a POJO.
Often, you want to accept a POJO and return a POJO. Use java.util.function.Function
to accept a single argument and return a single result.
import io.micronaut.function.FunctionBean;
import java.util.function.Function;
@FunctionBean("isbn-validator")
public class IsbnValidatorFunction implements Function<IsbnValidationRequest, IsbnValidationResponse> {
@Override
public IsbnValidationResponse apply(IsbnValidationRequest request) {
return new IsbnValidationResponse();
}
}
A single project can define multiple functions, however only a single function should be configured for execution by the application. This can be configured using the micronaut.function.name
property in application.yml
:
Configuring the Function Name to Execute
micronaut:
function:
name: hello-world-java
Alternatively you can specify the value when running your function (for example in the Dockerfile
) either as an environment variable:
Specifying the Function to Execute as a Environment variable
$ export MICRONAUT_FUNCTION_NAME=hello-world-java
$ java -jar build/libs/hello-world-function-all.jar
Or as a system property:
Specifying the Function to Execute as a System property
$ java -Dmicronaut.function.name=hello-world-java -jar build/libs/hello-world-function-all.jar