【问题标题】:How to form multiple get requests within a single rest api?如何在单个rest api中形成多个get请求?
【发布时间】:2020-11-27 16:26:05
【问题描述】:

我的代码中有以下获取请求:

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

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

如何进行仅激活其中一种方法的 get 调用?例如,我想进行仅激活“getWinningResponses()”方法的 get 调用。我正在使用邮递员进行测试。

请简单解释一下,我在spring和api方面非常非常新。

【问题讨论】:

  • 为什么不使用 '@GetMapping 编写第三种方法,基于你的 URL 路径,即 '@pathParam 或 '@QueryParam,你可以调用选择性方法,即 getGeneralResponses 或 getWinningResponses。

标签: java spring spring-boot rest


【解决方案1】:

这是一个非常普通的简化(我非常推荐一个教程 [eg. 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 之后。

【讨论】:

  • 太棒了。谢谢您的帮助!我理解这个想法。
猜你喜欢
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 2021-10-18
  • 2021-10-26
  • 2019-01-25
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多