Azure spring 引导功能-如何将触发器和输入绑定同时传递给 handleRequest 方法?
Azure spring boot function-how to pass trigger and input binding both to handleRequest method at same time?
package com.att.trace.function;
import java.util.Optional;
import com.att.trace.function.model.User;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.BlobInput;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.annotation.StorageAccount;
import org.springframework.cloud.function.adapter.azure.FunctionInvoker;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
public class HelloHandler extends FunctionInvoker<Tuple2<User, String>, 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();
System.out.println("requestjson=" + requestjson);
Tuple2<User, String> input = Tuples.of(user, requestjson);
handleRequest(input, context);
return request.createResponseBuilder(HttpStatus.OK).body("ok")
.header("Content-Type", "application/json").build();
}
}
package com.att.trace.function;
import java.util.function.Function;
import com.att.trace.function.model.User;
import org.springframework.stereotype.Component;
import reactor.util.function.Tuple2;
@Component
public class Hello implements Function<Tuple2<User, String>, String> {
public String apply(Tuple2<User, String> objects) {
System.out.println("objects.getT1()=" + objects.getT1());
System.out.println("objects.getT2()=" + objects.getT2());
String output = "test output";
return output;
}
}
当我创建 Http trigger 和 blobinput 的元组并将其发送到 handleRequest 时出现以下异常。
[2021-08-20T09:53:12.816Z] java.lang.UnsupportedOperationException:目前只有基于元组的函数支持多参数
[2021-08-20T09:53:12.818Z] 在
我需要将两个输入都发送到 handleRequest 方法...知道如何实现吗?
花了一些时间,我能实现你想要的唯一方法是使用 DTO class。
package com.example.demo.model;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class HelloInputArguments {
private User user;
private String content;
}
那么handler就简单了
package com.example.demo;
import com.example.demo.model.HelloInputArguments;
import com.example.demo.model.User;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.*;
import org.springframework.cloud.function.adapter.azure.FunctionInvoker;
import java.util.Optional;
public class HelloHandler extends FunctionInvoker<HelloInputArguments, String> {
@FunctionName("hello")
@StorageAccount("DefaultConnection")
public HttpResponseMessage execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
@BlobInput(name = "file", dataType = "string", path = "requestjson/proam.json") String content,
ExecutionContext context) {
User user = request.getBody().get();
return request.createResponseBuilder(HttpStatus.OK)
.body(handleRequest(new HelloInputArguments(user, content), context))
.header("Content-Type", "application/json")
.build();
}
}
和函数
package com.example.demo;
import com.example.demo.model.HelloInputArguments;
import org.springframework.stereotype.Component;
import java.util.function.Function;
@Component
public class Hello implements Function<HelloInputArguments, String> {
public String apply(HelloInputArguments input) {
return input.getContent();
}
}
I thin with Tuple are allowed only if you want to pass multiple Fluxs in doc
package com.att.trace.function;
import java.util.Optional;
import com.att.trace.function.model.User;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.BlobInput;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.annotation.StorageAccount;
import org.springframework.cloud.function.adapter.azure.FunctionInvoker;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
public class HelloHandler extends FunctionInvoker<Tuple2<User, String>, 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();
System.out.println("requestjson=" + requestjson);
Tuple2<User, String> input = Tuples.of(user, requestjson);
handleRequest(input, context);
return request.createResponseBuilder(HttpStatus.OK).body("ok")
.header("Content-Type", "application/json").build();
}
}
package com.att.trace.function;
import java.util.function.Function;
import com.att.trace.function.model.User;
import org.springframework.stereotype.Component;
import reactor.util.function.Tuple2;
@Component
public class Hello implements Function<Tuple2<User, String>, String> {
public String apply(Tuple2<User, String> objects) {
System.out.println("objects.getT1()=" + objects.getT1());
System.out.println("objects.getT2()=" + objects.getT2());
String output = "test output";
return output;
}
}
当我创建 Http trigger 和 blobinput 的元组并将其发送到 handleRequest 时出现以下异常。
[2021-08-20T09:53:12.816Z] java.lang.UnsupportedOperationException:目前只有基于元组的函数支持多参数 [2021-08-20T09:53:12.818Z] 在
我需要将两个输入都发送到 handleRequest 方法...知道如何实现吗?
花了一些时间,我能实现你想要的唯一方法是使用 DTO class。
package com.example.demo.model;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class HelloInputArguments {
private User user;
private String content;
}
那么handler就简单了
package com.example.demo;
import com.example.demo.model.HelloInputArguments;
import com.example.demo.model.User;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.*;
import org.springframework.cloud.function.adapter.azure.FunctionInvoker;
import java.util.Optional;
public class HelloHandler extends FunctionInvoker<HelloInputArguments, String> {
@FunctionName("hello")
@StorageAccount("DefaultConnection")
public HttpResponseMessage execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
@BlobInput(name = "file", dataType = "string", path = "requestjson/proam.json") String content,
ExecutionContext context) {
User user = request.getBody().get();
return request.createResponseBuilder(HttpStatus.OK)
.body(handleRequest(new HelloInputArguments(user, content), context))
.header("Content-Type", "application/json")
.build();
}
}
和函数
package com.example.demo;
import com.example.demo.model.HelloInputArguments;
import org.springframework.stereotype.Component;
import java.util.function.Function;
@Component
public class Hello implements Function<HelloInputArguments, String> {
public String apply(HelloInputArguments input) {
return input.getContent();
}
}
I thin with Tuple are allowed only if you want to pass multiple Fluxs in doc