SpringBootRequestHandler is now Deprecated

Published
Updated

Prior to version 3 of Spring Cloud, an application had to extend the SpringBootRequestHandler which provided a number of different request handler methods which could be overridden to define how your application functioned when invoked in a lambda-like context.

For example, here is what your codebase would have looked like prior to Spring Cloud version 3:

public class SpringBootLambdaHandler extends SpringBootRequestHandler<String, String> {
...
}

The project has since undergone some refactoring and restructuring so that Spring Cloud Functions are accessible directly from your app like so:

@SpringBootApplication
public class FuncAppExample {

    @Bean
    public Function<String, String> myExampleFunction() {
        return value -> value.toLowerCase();
    }


    public static void main(String[] args) {
        FunctionalSpringApplication.run(FuncAppExample.class, args);
    }
}

Alternatively, if you desire your function to start even quicker and need it to be more performant, you can define it as a functional bean definition as defined below. This requires a little bit more code but doesn’t rely on Spring’s component scan.

@SpringBootApplication
public class FuncAppExample implements ApplicationContextInitializer<GenericApplicationContext> {

    @Bean
    public Function<String, String> myExampleFunction() {
        return value -> value.toLowerCase();
    }

    @Override
    public void initialize(GenericApplicationContext context) {
        context.registerBean("myExampleFunction", FunctionRegistration.class,
        () -> new FunctionRegistration<Function<String, String>>(function())
        .type(FunctionType.from(String.class).to(String.class).getType()));
    }

    public static void main(String[] args) {
        FunctionalSpringApplication.run(FuncAppExample.class, args);
    }
}

Keep in mind if you have multiple Functions defined, you will need to specify which Function will get executed. Do so by adding the following to your application.properties file:

spring.cloud.function.definition=myExampleFunction