Java 如何在Azure中使用多个端点编写Spring云函数?
我正在尝试用SpringCloud创建2个Azure函数,但我无法让它工作Java 如何在Azure中使用多个端点编写Spring云函数?,java,spring,azure-functions,spring-cloud,spring-cloud-function,Java,Spring,Azure Functions,Spring Cloud,Spring Cloud Function,我正在尝试用SpringCloud创建2个Azure函数,但我无法让它工作 @配置 公共类FirstFunction扩展AzureSpringBootRequestHandler { @FunctionName(“firstFunction”) 公开募捐( @HttpTrigger(name=“req”,methods={HttpMethod.POST},authLevel=AuthorizationLevel.FUNCTION)HttpRequestMessage请求, 最终执行(上下文) {
@配置
公共类FirstFunction扩展AzureSpringBootRequestHandler
{
@FunctionName(“firstFunction”)
公开募捐(
@HttpTrigger(name=“req”,methods={HttpMethod.POST},authLevel=AuthorizationLevel.FUNCTION)HttpRequestMessage请求,
最终执行(上下文)
{
HandlerRequest(可选的.empty(),上下文);
}
@豆子
@懒惰的
函数firstFunction()
{
返回上下文->
{
//做第一功能的东西;
};
}
}
@配置
公共类SecondFunction扩展AzureSpringBootRequestHandler
{
@函数名(“第二个函数”)
公开募捐(
@HttpTrigger(name=“req”,methods={HttpMethod.POST},authLevel=AuthorizationLevel.FUNCTION)HttpRequestMessage请求,
最终执行(上下文)
{
HandlerRequest(可选的.empty(),上下文);
}
@豆子
@懒惰的
函数secondFunction()
{
返回上下文->
{
//做第二功能的东西;
};
}
}
@SpringBoot应用程序
公共类应用程序
{
公共静态void main(最终字符串[]args)
{
SpringApplication.run(Application.class,args);
}
}
使用上面的代码与spring cloud function dependencies 2.0.1.RELEASE
的依赖关系,在调用firstFunction
和secondFunction
端点时,它总是会点击firstFunction Bean
在做了一些谷歌搜索之后,我发现这建议转到2.1
但是,当我尝试更改为2.1.1.RELEASE
时,我遇到了一个异常,即找不到主类:
System.Private.CoreLib: Exception while executing function: Functions.extractContent. System.Private.CoreLib: Result: Failure
Exception: IllegalArgumentException: Failed to locate main class
Stack: java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry
in META-INF/MANIFEST.MF (in that order).
我做错了什么,需要一些帮助。我在我身边进行了测试,一切正常 您可以在以下位置获得我的演示:。该项目基于官方演示: 我的变化: HelloFunction.java
@springboot应用程序
公开课{
公共静态void main(字符串[]args)引发异常{
run(HelloFunction.class,args);
}
@憨豆(“你好”)
公共函数hello(){
return user->new问候语(“你好!欢迎,”+user.getName());
}
@比恩(“你好”)
公共职能hi(){
return user->new问候语(“嗨!欢迎,”+user.getName());
}
}
修改HelloHandler.java
公共类HelloHandler扩展AzureSpringBootRequestHandler{
@函数名(“你好”)
公众问候执行(
@HttpTrigger(name=“request”,methods={HttpMethod.GET,HttpMethod.POST},authLevel=AuthorizationLevel.ANONYMOUS)HttpRequestMessage请求,
ExecutionContext(上下文){
context.getLogger().info(“问候用户名:+request.getBody().get().getName());
返回handleRequest(request.getBody().get(),context);
}
}
添加HiHandler.java
公共类HiHandler扩展AzureSpringBootRequestHandler{
@函数名(“hi”)
公共问候语执行(@HttpTrigger(name=“request”,methods={HttpMethod.GET,
HttpMethod.POST},authLevel=AuthorizationLevel.ANONYMOUS)HttpRequestMessage请求,
ExecutionContext(上下文){
context.getLogger().info(“问候用户名:+request.getBody().get().getName());
返回handleRequest(request.getBody().get(),context);
}
}
运行函数:
mvn azure函数:运行
邮差测验
从函数hello:
从功能hi:
谢谢您!我还尝试将这两个bean放在一个单独的类中,并用配置进行注释。我认为我的原始代码的问题在于我把bean定义、自动连接(正如AzureSpringBootRequestHandler中暗示的那样)和配置都混在了一个类中。你知道为什么默认测试用例构建失败吗?如果我需要从函数hello方法访问ExecutionContext怎么办?我尝试自动连接上下文并将其作为参数提供。这可能吗?
@SpringBootApplication
public class HelloFunction {
public static void main(String[] args) throws Exception {
SpringApplication.run(HelloFunction.class, args);
}
@Bean("hello")
public Function<User, Greeting> hello() {
return user -> new Greeting("Hello! Welcome, " + user.getName());
}
@Bean("hi")
public Function<User, Greeting> hi() {
return user -> new Greeting("Hi! Welcome, " + user.getName());
}
}
public class HelloHandler extends AzureSpringBootRequestHandler<User, Greeting> {
@FunctionName("hello")
public Greeting execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
context.getLogger().info("Greeting user name: " + request.getBody().get().getName());
return handleRequest(request.getBody().get(), context);
}
}
public class HiHandler extends AzureSpringBootRequestHandler<User, Greeting> {
@FunctionName("hi")
public Greeting execute(@HttpTrigger(name = "request", methods = { HttpMethod.GET,
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
context.getLogger().info("Greeting user name: " + request.getBody().get().getName());
return handleRequest(request.getBody().get(), context);
}
}