Azure Spring Boot Function如何同时将触发器和输入绑定传入handleRequest方法
错误根因
你遇到的java.lang.UnsupportedOperationException: At the moment only Tuple-based function are supporting multiple arguments报错,是因为手动封装Reactor Tuple传入handleRequest时,Spring Cloud Function Azure适配器会将其识别为单个普通参数,不会当成多参数Tuple处理,因此无法匹配到对应的Function实现。
可行解决方案
方案1:自定义复合参数类(最推荐,兼容性最好)
不需要依赖Tuple,新增一个POJO类封装所有需要传入的参数,适配所有版本的Spring Cloud Function Azure适配器。
- 新增复合参数封装类
package com.att.trace.function.model; public class FunctionInput { private User user; private String requestJson; public FunctionInput(User user, String requestJson) { this.user = user; this.requestJson = requestJson; } // 补全Getter方法 public User getUser() { return user; } public String getRequestJson() { return requestJson; } }
- 调整Handler实现
public class HelloHandler extends FunctionInvoker<FunctionInput, String> { @FunctionName("hello") @StorageAccount("AzureWebJobsStorage") public HttpResponseMessage execute( @HttpTrigger(name = "request", methods = { HttpMethod.GET,HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request, @BlobInput(name = "requestjsonblob", dataType = "string", path = "requestjson/{name}.json") String requestjson, ExecutionContext context) { User user = request.getBody().get(); FunctionInput input = new FunctionInput(user, requestjson); handleRequest(input, context); return request.createResponseBuilder(HttpStatus.OK).body("ok") .header("Content-Type", "application/json").build(); } }
- 调整Spring Function实现
@Component public class Hello implements Function<FunctionInput, String> { public String apply(FunctionInput input) { System.out.println("user=" + input.getUser()); System.out.println("requestJson=" + input.getRequestJson()); return "test output"; } }
方案2:使用BiFunction适配(无需新增POJO)
Spring Cloud Function Azure适配器2.1.0及以上版本支持BiFunction类型的函数,可直接传入多参数,无需手动封装。
- 调整Spring Function实现为BiFunction
@Component public class Hello implements BiFunction<User, String, String> { public String apply(User user, String requestJson) { System.out.println("user=" + user); System.out.println("requestJson=" + requestJson); return "test output"; } }
- 调整Handler传参逻辑,直接传入参数数组即可
public class HelloHandler extends FunctionInvoker<Object, String> { @FunctionName("hello") @StorageAccount("AzureWebJobsStorage") public HttpResponseMessage execute( @HttpTrigger(name = "request", methods = { HttpMethod.GET,HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request, @BlobInput(name = "requestjsonblob", dataType = "string", path = "requestjson/{name}.json") String requestjson, ExecutionContext context) { User user = request.getBody().get(); // 直接传入参数数组,适配器会自动匹配BiFunction handleRequest(new Object[]{user, requestjson}, context); return request.createResponseBuilder(HttpStatus.OK).body("ok") .header("Content-Type", "application/json").build(); } }
注意:使用该方案需要确保
spring-cloud-function-adapter-azure依赖版本不低于2.1.0.RELEASE,避免出现版本兼容问题。
内容的提问来源于stack exchange,提问作者Tarun Aghara
相关产品推荐
相关产品推荐

