gpt4 book ai didi

Spring cloud function with as http end point with path variable(以路径变量作为Http终点的Spring Cloud函数)

转载 作者:bug小助手 更新时间:2023-10-28 10:56:21 25 4
gpt4 key购买 nike



I want to use Spring cloud function so I started it with as http end point. It works great but how do I use it with path variable. Say I want to make an end point like this: http://localhost:8080/myFunction/{id}?

我想使用Spring的云函数,所以我从http端点开始。它工作得很好,但如何将其与PATH变量一起使用。假设我想要这样做一个终点:http://localhost:8080/myFunction/{id}?


How to accomplish it via Spring cloud function

如何通过Spring云函数来实现


更多回答
优秀答案推荐

I am not sure where did you get org.springframework.cloud.function.web.FunctionRequest. It does not exist.
Also, why do you need MyFunctionController? I am struggling to understand what you are trying to accomplish. MyFunction is all you need to access http://localhost:8080/myFunction/123

我不知道你是从哪里弄到org.springframework.cloud.function.web.FunctionRequest.的它并不存在。另外,您为什么需要MyFunctionController?我正在努力理解你想要实现的目标。我的功能是您访问http://localhost:8080/myFunction/123所需的全部功能




  1. Assuming you are using Spring with Maven and a pom.xml specified you can include the dependency for Spring Cloud Function and Spring Web:


    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-function-web</artifactId>
    </dependency>
    </dependencies>






  1. Then try creating or altering your bean. This would create a simple Function that takes a String input (the path variable id) and returns a String response:


    import java.util.function.Function;
    import org.springframework.stereotype.Component;

    @Component
    public class MyFunction implements Function<String, String> {
    @Override
    public String apply(String id) {
    // Your function logic here
    return "Received ID: " + id;
    }
    }






  1. Next try creating or altering your controller to expose this function as an HTTP endpoint with a path variable. You can use the @RestController and @RequestMapping annotations for this. Below we injected the FunctionRequest for your MyFunction bean and create a GET mapping that accepts a path variable {id}. When a request is made to http://localhost:8080/myFunction/{id}, the handleRequest method is called, and it invokes your function with the provided id:


    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.function.web.FunctionRequest;
    import org.springframework.web.bind.annotation.*;

    @RestController
    @RequestMapping("/myFunction")
    public class MyFunctionController {
    @Autowired
    private FunctionRequest<MyFunction> functionRequest;

    @GetMapping("/{id}")
    public String handleRequest(@PathVariable String id) {
    return functionRequest.apply(id);
    }
    }






  1. Finally try running your Spring Boot application, and you can access your function using the endpoint, http://localhost:8080/myFunction/123.


Hope this helps!

希望这个能帮上忙!


更多回答

so 123 is injected as @Pathvariable in spring MVC?

那么123在Spring MVC中是作为@Path变量注入的吗?

What Spring MVC? The question is about Spring Cloud Function which is not Spring MVC

什么Spring MVC?问题是关于Spring Cloud函数,它不是Spring MVC

Yes of course I just trying to make comparison between Spring cloud function and Spring MVC to better understand how Spring cloud function works as http end point

是的,当然,我只是想比较一下Spring Cloud函数和Spring MVC,以便更好地理解Spring Cloud函数作为http端点是如何工作的

FunctionRequest is not recognize...I use Spring boot 3.1

无法识别FunctionRequest...我使用的是Spring Boot 3.1

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com