gpt4 book ai didi

java - Azure Spring Boot功能-如何将触发器和输入绑定(bind)同时传递给handleRequest方法?

转载 作者:行者123 更新时间:2023-12-03 02:21:12 30 4
gpt4 key购买 nike

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 触发器和 blobinput 的元组并将其发送到handleRequest 时出现异常。

[2021-08-20T09:53:12.816Z] java.lang.UnsupportedOperationException:目前只有基于元组的函数支持多个参数[2021-08-20T09:53:12.818Z] 位于

我需要将两个输入发送到handleRequest 方法...有什么线索可以实现这一点吗?

最佳答案

花了一些时间,我实现你想要的唯一方法是使用 DTO 类。

package com.example.demo.model;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class HelloInputArguments {
private User user;
private String content;
}

那么处理程序就很简单

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();
}
}

只有当你想传递多个 Flux 时才允许使用 Tuple in doc

关于java - Azure Spring Boot功能-如何将触发器和输入绑定(bind)同时传递给handleRequest方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68860401/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com