Spring 和 Azure 函数 [英] Spring and Azure function

查看:11
本文介绍了Spring 和 Azure 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 是否适用于 Azure 函数?

Does Spring work with Azure functions?

例如:Rest API 里面的代码使用Autowired"注解(运行 mvn azure-functions:run 后,myScriptService"出现 NullPointerException).

For example: Rest API that the code inside uses "Autowired" annotation (After running mvn azure-functions:run I've got NullPointerException on "myScriptService").

import java.util.*;
import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.*;
import com.company.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {

    @Autowired
    ScriptService myScriptService;
    /**
     * This function listens at endpoint "/api/hello". Two ways to invoke it using "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/hello
     * 2. curl {your host}/api/hello?name=HTTP%20Query
     */
    @FunctionName("myhello")
    public HttpResponseMessage<String> hello(
            @HttpTrigger(name = "req", 
                        methods = "post", 
                        authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
                        final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        // Parse query parameter
        String query = request.getQueryParameters().get("name");

        String name = request.getBody().orElse(query);                


        if (name == null) {
            return request.createResponse(400, "Please pass a name on the query string or in the request body");
        } else {
            return request.createResponse(200, "Hello, " + name  + ", myScriptService.isEnabled(): " + myScriptService.isEnabled());
        }
    }
}

推荐答案

正如一些人在上面的评论中要求解决方案,我假设这个问题也可能与其他用户有关.

As some asked for a solution in the comments above, I'm assuming that this problem might be of relevance for other users, too.

所以我认为 Spring Cloud Function 是这里的神奇词汇:除了其他几点(参见 项目页面了解详细信息),它旨在在无服务器提供商(除了 Azure Functions 之外,还支持 AWS Lambda 和 Apache OpenWhisk)上启用 Spring Boot 功能(如依赖注入,您正在寻找的东西).

So I think Spring Cloud Function is the magic word here: besides some other points (see the project page for details), it aims to enable Spring Boot features (like dependency injection, what you're looking for) on serverless providers (besides Azure Functions, also AWS Lambda and Apache OpenWhisk are supported).

所以你必须对你的项目做一些修改:添加 spring-cloud-function-adapter-azure 依赖:

So you have to make some modifications to your project: Add the spring-cloud-function-adapter-azure dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-function-adapter-azure</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

您的处理程序类需要一些额外的代码:

Your handler class needs some additional code:

  • 添加@SpringBootApplication 注解
  • 添加 Spring Boot 应用程序中已知的 main() 方法
  • 确保 Spring 可以找到您的 ScriptService 类 e.G.通过使用@ComponentScan 注释

应该是这样的:

@SpringBootApplication
@ComponentScan(basePackages = { "package.of.scriptservice" })
public class Function {
    @Autowired
    ScriptService myScriptService;

    @FunctionName("myhello")
    public HttpResponseMessage<String> hello(
            @HttpTrigger(name = "req", methods = "post", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {

        // Your code here
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoFunctionHandler.class, args);
    }
}

您可以在此处找到完整示例和 这里

You can find a full example here and here

这篇关于Spring 和 Azure 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆