Java 将多个参数传递给rest API - Spring
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38032635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass multiple parameters to rest API - Spring
提问by Saif Masadeh
I am trying to figure out if it is possible to pass a JSON object to rest API, Or pass a multiple parameters to that API ? And how to read these parameters in Spring ? Lets assume that the url looks like the below examples :
我想弄清楚是否可以将 JSON 对象传递给 rest API,或者将多个参数传递给该 API?以及如何在 Spring 中读取这些参数?让我们假设 url 看起来像下面的例子:
Ex.1 http://localhost:8080/api/v1/mno/objectKey?id=1&name=saif
例 1 http://localhost:8080/api/v1/mno/objectKey?id=1&name=saif
Is it valid to pass a JSON object like in the url below ?
传递如下 url 中的 JSON 对象是否有效?
Ex.2 http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}
例2 http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}
Questions:
问题:
1) Is it possible to pass a JSON object to the url like in Ex.2?
1) 是否可以像 Ex.2 那样将 JSON 对象传递给 url?
2) How can we pass and parse the parameters in Ex.1?
2)我们如何传递和解析Ex.1中的参数?
I tried to write some methods to achieve my goal, but could not find the right solution?
我尝试编写一些方法来实现我的目标,但找不到正确的解决方案?
I tried to pass JSON object as @RequestParam
我试图将 JSON 对象作为 @RequestParam 传递
http://localhost:8080/api/v1/mno/objectKey?id=1
There was an unexpected error (type=Unsupported Media Type, status=415). Content type 'null' not supported
http://localhost:8080/api/v1/mno/objectKey?id=1
出现意外错误 (type=Unsupported Media Type, status=415). Content type 'null' not supported
http://localhost:8080/api/v1/mno/objectKey/id=1
There was an unexpected error (type=Not Found, status=404). No message available
http://localhost:8080/api/v1/mno/objectKey/id=1
出现意外错误 (type=Not Found, status=404). No message available
http://localhost:8080/api/v1/mno/objectKey/%7B%22id%22:1%7D
There was an unexpected error (type=Not Found, status=404). No message available
http://localhost:8080/api/v1/mno/objectKey/%7B%22id%22:1%7D
出现意外错误 (type=Not Found, status=404). No message available
@RequestMapping(value="mno/{objectKey}",
method = RequestMethod.GET,
consumes="application/json")
public List<Book> getBook4(@RequestParam ObjectKey objectKey) {
...
}
I tried to pass the JSON object as @PathVariable
我试图将 JSON 对象作为 @PathVariable 传递
@RequestMapping(value="ghi/{objectKey}",method = RequestMethod.GET)
public List<Book> getBook2(@PathVariable ObjectKey objectKey) {
...
}
I created this object to hold the id parameter and other parameters like name , etc ....
我创建了这个对象来保存 id 参数和其他参数,如 name 等......
class ObjectKey{
long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
采纳答案by Andreas
(1) Is it possible to pass a JSON object to the url like in Ex.2?
(1) 是否可以像 Ex.2 那样将 JSON 对象传递给 url?
No, because http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}
is not a valid URL.
不,因为http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}
不是有效的 URL。
If you want to do it the RESTful way, use http://localhost:8080/api/v1/mno/objectKey/1/Saif
, and defined your method like this:
如果您想以 RESTful 方式执行此操作,请使用http://localhost:8080/api/v1/mno/objectKey/1/Saif
并像这样定义您的方法:
@RequestMapping(path = "/mno/objectKey/{id}/{name}", method = RequestMethod.GET)
public Book getBook(@PathVariable int id, @PathVariable String name) {
// code here
}
(2) How can we pass and parse the parameters in Ex.1?
(2)如何传递和解析Ex.1中的参数?
Just add two request parameters, and give the correct path.
只需添加两个请求参数,并给出正确的路径。
@RequestMapping(path = "/mno/objectKey", method = RequestMethod.GET)
public Book getBook(@RequestParam int id, @RequestParam String name) {
// code here
}
UPDATE(from comment)
更新(来自评论)
What if we have a complicated parameter structure ?
"A": [ { "B": 37181, "timestamp": 1160100436, "categories": [ { "categoryID": 2653, "timestamp": 1158555774 }, { "categoryID": 4453, "timestamp": 1158555774 } ] } ]
如果我们有一个复杂的参数结构怎么办?
"A": [ { "B": 37181, "timestamp": 1160100436, "categories": [ { "categoryID": 2653, "timestamp": 1158555774 }, { "categoryID": 4453, "timestamp": 1158555774 } ] } ]
Send that as a POST
with the JSON data in the request body, not in the URL, and specify a content type of application/json
.
将其作为POST
JSON 数据发送到请求正文中,而不是在 URL 中,并指定内容类型application/json
.
@RequestMapping(path = "/mno/objectKey", method = RequestMethod.POST, consumes = "application/json")
public Book getBook(@RequestBody ObjectKey objectKey) {
// code here
}
回答by techspl
Yes its possible to pass JSON object in URL
是的,可以在 URL 中传递 JSON 对象
queryString = "{\"left\":\"" + params.get("left") + "}";
httpRestTemplate.exchange(
Endpoint + "/A/B?query={queryString}",
HttpMethod.GET, entity, z.class, queryString);
回答by Om Sharma
you can pass multiple params in url like
您可以在 url 中传递多个参数,例如
http://localhost:2000/custom?brand=dell&limit=20&price=20000&sort=asc
http://localhost:2000/custom?brand=dell&limit=20&price=20000&sort=asc
and in order to get this query fields , you can use map like
为了获得这个查询字段,你可以使用像这样的地图
@RequestMapping(method = RequestMethod.GET, value = "/custom")
public String controllerMethod(@RequestParam Map<String, String> customQuery) {
System.out.println("customQuery = brand " + customQuery.containsKey("brand"));
System.out.println("customQuery = limit " + customQuery.containsKey("limit"));
System.out.println("customQuery = price " + customQuery.containsKey("price"));
System.out.println("customQuery = other " + customQuery.containsKey("other"));
System.out.println("customQuery = sort " + customQuery.containsKey("sort"));
return customQuery.toString();
}
回答by Rohit Dubey
Multiple parameters can be given like below,
可以给出多个参数,如下所示,
@RequestMapping(value = "/mno/{objectKey}", method = RequestMethod.GET, produces = "application/json")
public List<String> getBook(HttpServletRequest httpServletRequest, @PathVariable(name = "objectKey") String objectKey
, @RequestParam(value = "id", defaultValue = "false")String id,@RequestParam(value = "name", defaultValue = "false") String name) throws Exception {
//logic
}