将多个参数传递给rest API – Spring

我想弄清楚是否可以将JSON对象传递给其他API,或者将多个参数传递给该API? 以及如何在Spring中读取这些参数? 让我们假设url如下所示:

例1 http://localhost:8080/api/v1/mno/objectKey?id=1&name=saif

在下面的url中传递JSON对象是否有效?

例2 http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}

问题:

1)是否可以像在Ex.2中一样将JSON对象传递给url?

2)我们如何传递和解析Ex.1中的参数?

我试着写一些方法来实现我的目标,但找不到合适的解决方案?

我试图将JSON对象作为@RequestParam传递

http://localhost:8080/api/v1/mno/objectKey?id=1出现意外错误(type=Unsupported Media Type, status=415). Content type 'null' not supported (type=Unsupported Media Type, status=415). Content type 'null' not supported

http://localhost:8080/api/v1/mno/objectKey/id=1出现意外错误(type=Not Found, status=404). No message available (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 (type=Not Found, status=404). No message available

 @RequestMapping(value="mno/{objectKey}", method = RequestMethod.GET, consumes="application/json") public List getBook4(@RequestParam ObjectKey objectKey) { ... } 

我试图将JSON对象作为@PathVariable传递

 @RequestMapping(value="ghi/{objectKey}",method = RequestMethod.GET) public List getBook2(@PathVariable ObjectKey objectKey) { ... } 

我创建了这个对象来保存id参数和其他参数,如名称等….

 class ObjectKey{ long id; public long getId() { return id; } public void setId(long id) { this.id = id; } } 

(1)是否可以像在Ex.2中那样将JSON对象传递给url?

不,因为http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"}不是有效的URL。

如果你想以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)我们如何传递和解析Ex.1中的参数?

只需添加两个请求参数,并提供正确的路径。

 @RequestMapping(path = "/mno/objectKey", method = RequestMethod.GET) public Book getBook(@RequestParam int id, @RequestParam String name) { // code here } 

更新 (来自评论)

如果我们有一个复杂的参数结构怎么办?

 "A": [ { "B": 37181, "timestamp": 1160100436, "categories": [ { "categoryID": 2653, "timestamp": 1158555774 }, { "categoryID": 4453, "timestamp": 1158555774 } ] } ] 

使用请求正文中的JSON数据将其作为POST发送,而不是在URL中,并指定application/json的内容类型。

 @RequestMapping(path = "/mno/objectKey", method = RequestMethod.POST, consumes = "application/json") public Book getBook(@RequestBody ObjectKey objectKey) { // code here } 

你可以在url中传递多个参数

HTTP://本地主机:2000 /定制品牌=戴尔和限制= 20&价格= 20000&排序= ASC

并且为了获得此查询字段,您可以使用map like

  @RequestMapping(method = RequestMethod.GET, value = "/custom") public String controllerMethod(@RequestParam Map 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(); } 

是的,可以在URL中传递JSON对象

 queryString = "{\"left\":\"" + params.get("left") + "}"; httpRestTemplate.exchange( Endpoint + "/A/B?query={queryString}", HttpMethod.GET, entity, z.class, queryString);