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 :

Ex.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 ?

例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?

2) How can we pass and parse the parameters in Ex.1?

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

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

发生了一个意外的错误

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/%7B%22id%22:1%7D There was an unexpected error (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

@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 ....

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

推荐答案

(1) Is it possible to pass a JSON object to the url like in Ex.2?

No, because http://localhost:8080/api/v1/mno/objectKey/{"id":1, "name":"Saif"} is not a valid 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
}

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

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.

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

Json相关问答推荐

JoltChange:将输入数组的每个对象拆分为2个独立的对象,并将其放入输出数组中

jq不会为空输入返回非零

JOLT拉平数组

在scala中将字符串列转换为 struct 的JSON映射

使用JQ在数组中 Select 第一个具有匹配项的项

递归解码嵌套列表(具有任意深度的列表列表)

Python 将 struct 化文本转换和筛选为对象

如何使用 jq 将字符串数组转换为对象?

从 Inno Setup 中的 JSON 文件的每个对象中读取特定字符串

try 使用 JQ 转换 JSON 中过于复杂的对象数组

如何在jolt中使用shift和modify-overwrite-beta

从 PowerShell 编辑 Windows 终端配置文件设置 JSON

如何在 Dart 中与多个 map (字典)相交

如果 JSON 对象包含列表中的子字符串,则丢弃它们

下一个 Js 部署在 getStaticPath 函数上失败:在 JSON.parse 的位置 0 的 JSON 中出现意外的令牌 < 但在本地运行

在 JavaScript 中从 Json 数据中删除反斜杠

在 Http Header 中使用 Json 字符串

jQuery fullcalendar 发送自定义参数并使用 JSON 刷新日历

NSManagedObject 属性值的 NSNull 处理

通过url获取json数据并在python中使用(simplejson)