如何在REST API中的单个Queryparameter中实现多个值?

发布于 2025-01-25 02:46:22 字数 130 浏览 1 评论 0原文

例如: http://主机:port/port/template/products/?productid = 1234,1235 如何实现上述URL的REST API,其中单个Queryparam通过多个值并获取Productids的所有现有记录

eg:
http://host:port/template/products/?productid=1234,1235
how to implement rest api for the above url where in single queryparam passing multiple values and get the all existing records for the productids

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑梦风尘 2025-02-01 02:46:22

尝试下面的事情来完成您要尝试的事情:

    @RequestMapping(value="/products", method=RequestMethod.GET)
    public String getMultipleProductsInfo(@RequestParam List<Long> productIdList) {
     
          String productsInfoAsString = getExistingRecordsForTheProductIds(productIdList);
          return productsInfoAsString;
     }

注意

@requestParam列表productidlist

逗号分隔的产品ID列表被复制到ProductIdlist列表对象中。您可以迭代列表,也可以将其发送到负责从数据源中获取结果的方法,例如数据库,

请注意,上面的代码shippet将响应返回为字符串。但是您也可以使API也以其他格式返回响应,例如JSON。

Try something like below to accomplish what you are trying to do:

    @RequestMapping(value="/products", method=RequestMethod.GET)
    public String getMultipleProductsInfo(@RequestParam List<Long> productIdList) {
     
          String productsInfoAsString = getExistingRecordsForTheProductIds(productIdList);
          return productsInfoAsString;
     }

Notice the

@RequestParam List productIdList

The comma separated list of product IDs are copied into the productIdList list object. You can either iterate through the list or send it over to the method that is responsible for fetching the results from the datasource, such as your database

Please note that the above code snippet returns the response as String. But you could make your API return a response in other formats too, such as JSON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文