Saif Masadeh Saif Masadeh - 5 months ago 96
JSON Question

Pass multiple parameters to rest API - Spring

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 ?

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

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

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

Answer

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

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:

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

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

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
}