首页 > 解决方案 > 如何为 azurei 设置 i 响应状态 n Sprint 云功能

问题描述

我是 Azure Functions 的新手,我想将 Spring Cloud Function 与 Azure Function 集成,我参考了很多文章,但无法获得有关如何设置 HTTP 响应状态和自定义使用 Spring Cloud 从 API 生成的内容的任何信息。

我正在使用以下 Maven 依赖项:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-function-adapter-azure</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-function-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <!--<version>5.0.10.RELEASE</version>-->
</dependency>

这应该在 Azure Serverless 平台上运行。

1:弹簧功能码:

public class AuthorizationFunction implements Function<String,ServicesOut> {

    @Autowired
    private someservice;


    @Override
    public DomainObject apply(String entitlement) {

    }
} 

2:集成Azure的Spring Function Handler

public class EntitlementHandler extends AzureSpringBootRequestHandler<String, ServicesOut> {

    @FunctionName("functionname")
    public DomainObject execute(@HttpTrigger(name = "request", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage request,
             ExecutionContext context) {

     // Depending on Function.apply value set httpresponse status and details
     // set 201 when success
     // set 401 when failure
     // set 500 for other failures
   }
}

任何帮助都感激不尽。

标签: javaazureazure-functions

解决方案


您可以通过返回来自定义您的响应HttpResponseMessage。更多细节可以参考:HttpRequestMessage 和 HttpResponseMessage

这是我的测试:

1. 下载带有spring函数示例的Azure函数

git clone https://github.com/Azure-Samples/hello-spring-function-azure.git

2.修改类如下

类 HelloHandler

public class HelloHandler extends AzureSpringBootRequestHandler<HttpRequestMessage<Optional<User>>, HttpResponseMessage> {

    @FunctionName("hello")
    public HttpResponseMessage 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, context);
    }
}

类 hello() 函数 bean

@SpringBootApplication
public class HelloFunction {

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

    @Bean
    public Function<HttpRequestMessage<Optional<User>>, HttpResponseMessage> hello() {
        return request -> request.createResponseBuilder(HttpStatus.ACCEPTED).body(request.getBody().get().getName()).build();
    }
}

3. 结果

当我 chenged 函数时,我删除了所有的测试类。因为它不再匹配了。如果你需要测试,你应该改变测试逻辑。

然后我只是在终端中运行该函数:mvn clean package azure-functions:run

然后我发出了一个http请求,得到了以下响应:

在此处输入图像描述

可以看到响应http状态是“Accepted”,和我在函数中设置的一样。


推荐阅读