2
votes

I am experimenting with Spring Cloud Functions on top of Azure Functions, and while I was trying to run a sample project for azure functions I came across the following error.

 Executed 'Functions.uppercase' (Failed, Id=4fd6ef4c-209c-45cb-bf65-ce38e90b6620)
 System.Private.CoreLib: Exception while executing function: Functions.uppercase. System.Private.CoreLib: Result: Failure
 Exception: ClassCastException: com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource cannot be cast to java.lang.String
 Stack: java.lang.ClassCastException: com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource cannot be cast to java.lang.String
   at com.microsoft.azure.functions.worker.binding.DataOperations.apply(DataOperations.java:91)
   at com.microsoft.azure.functions.worker.binding.DataSource.computeByType(DataSource.java:56)
   at com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource.computeByType(RpcHttpRequestDataSource.java:20)
   at com.microsoft.azure.functions.worker.binding.DataSource.computeByName(DataSource.java:42)
   at com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource.computeByName(RpcHttpRequestDataSource.java:20)
   at com.microsoft.azure.functions.worker.binding.BindingDataStore.getDataByName(BindingDataStore.java:50)
   at com.microsoft.azure.functions.worker.broker.ParameterResolver.resolve(ParameterResolver.java:59)
   at com.microsoft.azure.functions.worker.broker.ParameterResolver.resolve(ParameterResolver.java:42)
   at com.microsoft.azure.functions.worker.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:52)
   at com.microsoft.azure.functions.worker.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:51)
   at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
   at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
   at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:45)
   at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:92)
   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
   at java.lang.Thread.run(Thread.java:748)
 .

Sample project link on github https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-azure

public class FooHandler extends AzureSpringBootRequestHandler<Foo, Bar> {
    @FunctionName("uppercase")
    public Bar execute(
            @HttpTrigger(name = "req", methods = { HttpMethod.GET,
                    HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) Foo foo,
            ExecutionContext context) {
        return handleRequest(foo, context);
    }  
}

Azure local runtime version 2 (Fails in Azure as well)

Azure Functions Core Tools (2.3.199 Commit hash: fdf734b09806be822e7d946fe17928b419d8a289)
Function Runtime Version: 2.0.12246.0

As there's nothing related to Spring Cloud in the stack-trace, I suppose this is related to Azure. Would appreciate any help.

2
The error pretty clearly states that com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource cannot be cast to java.lang.String.rickvdbosch

2 Answers

1
votes

Have a look at this issue on GitHub: byte[] input broken.

Thank you reporting. This is a regression. As a workaround, please use java.lang.Byte[] instead of byte[]

Looks like it has been fixed:

pragnagopa closed this in #241 on Nov 14, 2018

0
votes

The sample code given at https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-azure has some issues.

The type of first parameter should be HttpRequestMessage<Optional<Foo>> and not Foo.

@FunctionName("uppercase")
    public Bar execute(
        @HttpTrigger(name = "req", methods = {HttpMethod.GET,
            HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<Foo>> fooHttp,
        ExecutionContext context) {
        Foo foo = fooHttp.getBody().get();
        return handleRequest(foo, context);
    }

Also the version of spring-cloud-function-dependencies had to be changed to 2.0.1.RELEASE.

mvn clean package azure-functions:run was then running the code correctly for me.