Java 如何在azurei的Sprint云函数中设置i响应状态
我是Azure函数的新手,我想将SpringCloud函数与Azure函数集成,我参考了许多文章,但无法获得有关如何使用SpringCloud设置HTTP响应状态和自定义API生成的内容的任何信息 我使用以下Maven依赖项:Java 如何在azurei的Sprint云函数中设置i响应状态,java,azure,azure-functions,Java,Azure,Azure Functions,我是Azure函数的新手,我想将SpringCloud函数与Azure函数集成,我参考了许多文章,但无法获得有关如何使用SpringCloud设置HTTP响应状态和自定义API生成的内容的任何信息 我使用以下Maven依赖项: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-adapter-azure<
<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>
org.springframework.cloud
spring云函数适配器azure
org.springframework.cloud
SpringCloudStarter功能web
org.springframework.boot
弹簧启动机tomcat
假如
org.springframework
弹簧网
这应该在Azure无服务器平台上运行
1:弹簧功能代码:
public class AuthorizationFunction implements Function<String,ServicesOut> {
@Autowired
private someservice;
@Override
public DomainObject apply(String entitlement) {
}
}
公共类授权函数实现函数{
@自动连线
私人服务;
@凌驾
公共域对象应用(字符串权限){
}
}
2:Spring函数处理程序集成Azure
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
}
}
公共类授权处理程序扩展AzureSpringBootRequestHandler{
@函数名(“函数名”)
public DomainObject execute(@HttpTrigger(name=“request”,methods={HttpMethod.GET},authLevel=AuthorizationLevel.ANONYMOUS)HttpRequestMessage请求,
ExecutionContext(上下文){
//根据Function.apply值设置httpresponse状态和详细信息
//成功时设置201
//失败时设置401
//为其他故障设置500
}
}
任何帮助都将不胜感激。您可以通过返回
HttpResponseMessage
自定义您的响应。有关更多详细信息,请参阅:
这是我的测试:
1.下载Azure函数和spring函数示例
2.修改类,如下所示
高级管理人员
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);
}
}
公共类HelloHandler扩展AzureSpringBootRequestHandler{
@函数名(“你好”)
公共httpresponsemessageexecute(
@HttpTrigger(name=“request”,methods={HttpMethod.GET,HttpMethod.POST},authLevel=AuthorizationLevel.ANONYMOUS)HttpRequestMessage请求,
ExecutionContext(上下文){
context.getLogger().info(“问候用户名:+request.getBody().get().getName());
返回handleRequest(请求、上下文);
}
}
类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();
}
}
@springboot应用程序
公开课{
公共静态void main(字符串[]args)引发异常{
run(HelloFunction.class,args);
}
@豆子
公共函数hello(){
return request->request.createResponseBuilder(HttpStatus.ACCEPTED).body(request.getBody().get().getName()).build();
}
}
3.后果
当我转换函数时,我删除了所有的测试类。因为它不再匹配了。如果你需要测试,你应该改变测试逻辑
然后我在终端中运行了函数:mvn clean package azure functions:run
然后我发出了一个http请求,得到了以下响应:
您可以看到响应http状态为“已接受”,这与我在函数中设置的是正确的
@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();
}
}