简体   繁体   中英

Spring Cloud Function: How to map a Supplier to Azure function

I'm trying to map a Supplier Bean to an Azure function using Spring Cloud Function 2.0, but I need to extend AzureSpringBootRequestHandler, which seems to only support functions with an input parameter and a return value. class AzureSpringBootRequestHandler has two type parameters: input and output, and AzureSpringBootRequestHandler.handleRequest() also expects the input parameter.

@Bean
public Supplier<List<String>> foo() {
    return () -> Arrays.asList("foo1", "foo2");
}

/////

class FooFunction extends AzureSpringBootRequestHandler<Void, List<String>> {
    @FunctionName("foo")
    List<String> foo(@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST},
        authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
                               ExecutionContext context) {
        return handleRequest(null, context);
    }
}

The code above causes NPE at reactor.core.publisher.FluxJust.(FluxJust.java:60)

Changing the @Bean return type to Function<Void, List<String>> causes IllegalStateException "No function defined with name=foo" at AzureSpringFunctionInitializer.lookup

Adding a dummy int parameter works.

PS Ideally I don't even need the return value so instead of Supplier I would make it a Runnable, but this seems completely unsupported.

Any help would be appreciated.

Support for supplier and consumer is added in Spring Cloud Function 3.0.0. This is currently still a milestone.

More information this change .

I solved the issue, using Spring Cloud Function 2.x, by changing the signature of AzureSpringBootRequestHandler to use Optional as follows:

public class SomeFunction extends AzureSpringBootRequestHandler<Optional<?>, List<Foo>> {

    @FunctionName("some-function")
    public List<Device> execute(@HttpTrigger(name = "req",
            methods = {HttpMethod.GET},
            authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Void> request,
            ExecutionContext context) {
        return handleRequest(Optional.empty(), context);
    }
}

You'll also have to change the type of your bean to match this:

@Bean(name="some-function")
public Function<Optional<?>, List<Device>> someFunction() {
    return v -> fooService.bar();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM