java - 如何为 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
}
}
任何帮助都感激不尽。
解决方案
您可以通过返回来自定义您的响应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”,和我在函数中设置的一样。
推荐阅读
- elixir - 从另一个字段推断字段值
- android - Android 9 配置设备和模拟器中的 API 调用显示连接错误
- google-cloud-firestore - 配额用完后,Firestore 看不到数据
- c# - 组合框项目选择更改时重新加载 DataGridview
- android - 尝试将数据加载到共享首选项和从共享首选项加载时应用程序崩溃
- android - 如何通过 Braintree 支付网关支付 INR
- node.js - 无法读取未定义的 node.js 文件系统的属性“toString”
- react-native - 在 React-Native 中,当将 appcenter 与 i18n 一起使用时,重复的 react-native 错误
- php - laravel 5.6 - 连接被拒绝
- css - Bootstrap 3,媒体查询清除:左