将整数列表传递给GET REST API [英] Passing List of Integers to GET REST API

查看:96
本文介绍了将整数列表传递给GET REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从前端数据库中获取实体列表. 因此,我在Spring MVC中编写了POST REST HTTP调用.

I wanted to fetch the List of Entities from database at Front end. So I have written POST REST HTTP call in Spring MVC.

但是我阅读了HTTP文档,该文档说,每当您必须从数据库检索数据时,首选GET调用. 因此,是否有什么我可以将Angular JS的POST调用替换为GET调用,并传递Integers列表. 但是,GET HTTP有很多缺点,例如:URL的长度受到限制.考虑到必须从数据库中获取1000个实体的情况.

But I read the HTTP documentation which says whenever you have to retrieve data from database prefer GET call. So, Is it there is any I can replace the POST call to GET call from angular JS and pass list of Integers. But, GET HTTP has many drawbacks like : the length of URL is limited.Considering the case where we have to fetch 1000 entities from database.

请向我建议在Spring MVC中获取实体或编写GET REST API的整数列表(指实体ID)的可能方法.

例如:考虑在书桌上有100本书,但我只想几本书,比如id:5,65,42,10,53,87,34,23. 这就是为什么我在POST调用中将这个ID列表传递到Integer列表中的原因.

For Example : Consider there are 100 books in book table, But I want only few books, say id : 5,65,42,10,53,87,34,23. Thats why I am passing this List of Id's in a List of Integer in POST call.

当前陷入了如何将其转换为GET调用的问题.简而言之,如何通过GET REST调用传递整数列表.

Currently stuck how to convert this to GET call. In Short, how to pass List of Integers through GET REST call.

推荐答案

由于 http:/之后传递资源ID /../resource/id "和HTTP参数用于过滤.

I prefer a variant through HTTP path variable for your problem, because of in REST ideology a resource ID is passed after a resource name 'http://../resource/id' and HTTP parameters are used for filtering.

如果您需要通过HTTP参数传递ID,请参见以下示例:

If you need to pass your ids through HTTP parameters, see an axample below:

这是您的Spring MVC控制器方法:

Here is your Spring MVC controller method:

@RequestMapping(value = "/books", params = "ids", method = RequestMethod.GET)
@ResponseBody
Object getBooksById_params(@RequestParam List<Integer> ids) {
    return "ids=" + ids.toString();
}

您可以使用下一个变体进行呼叫:

And you can make a call using next variants:

  1. http://server:port/ctx/books?ids = 5, 65,42
  2. http://server:port/ctx/books? ids = 5& ids = 65& ids = 42
  1. http://server:port/ctx/books?ids=5,65,42
  2. http://server:port/ctx/books?ids=5&ids=65&ids=42

也请看一下讨论: https://stackoverflow.com/a/9547490/1881761

您还可以通过路径变量传递ID,请参见以下示例:

Also you can pass your ids through path variable, see an example below:

@RequestMapping(value = "/books/{ids}", method = RequestMethod.GET)
@ResponseBody
Object getBooksById_pathVariable(@PathVariable List<Integer> ids) {
    return "ids=" + ids.toString();
}

您的呼叫将如下所示: http://server:port/ctx /books/5,65,42

And your call will be look like this: http://server:port/ctx/books/5,65,42

这篇关于将整数列表传递给GET REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆