有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在一个rest api中形成多个get请求?

我的代码中有以下get请求:

@GetMapping
public ArrayList<Response> getGeneralResponses() {
        return requestService.getGeneralResponses();
}

@GetMapping
public ArrayList<Response> getWinningResponses() {
        return requestService.getWinningResponses();
}

我如何拨打仅激活其中一种方法的get电话?例如,我想进行一个get调用,只激活“getWinningResponses()”方法。我在用邮递员做测试

请简单地解释一下,我对spring和api都非常、非常陌生


共 (1) 个答案

  1. # 1 楼答案

    这是一个非常普通的简化(我非常推荐一个教程[例如Baeldung],更不用说阅读官方文件):

    在类注释和/或方法注释中指定端点(指向公开资源的路径):

    @Controller("/path1")
    public class MyController {
        @GetMapping
        public String getGreeting() {
            return "Hello";
        }
    
        @GetMapping("/path2")
        public String getAnother() {
            return "Another";
        }
    }
    

    这样,如果你的应用程序在端口8080上运行,在访问http://localhost:8080/path1之后,你会看到Hellohttp://localhost:8080/path1/path2-Another