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

新手上路,请多包涵

我想弄清楚是否可以将 JSON 对象传递给 rest API,或者将多个参数传递给该 API?以及如何在 Spring 中读取这些参数?让我们假设 url 看起来像下面的例子:

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

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

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

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 出现意外错误 (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) {
        ...
    }

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

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

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

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

原文由 Saif 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 762
2 个回答

(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
>     } ]
> } ]
>
> ```

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

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

”`

原文由 Andreas 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

http://localhost:2000/custom?brand=dell&limit=20&price=20000&sort=asc

为了得到这个查询字段,你可以使用像这样的地图

    @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();
    }

原文由 Om Prakash Sharma 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题