0

in a spring web application I'm trying to send object list as request data. I can send the json object list in message body and parse in POST but I'm having trouble send the same data through url for GET. in json the list structure is as follows

  {
"vendorIDs": [{
"vendorID": [111,1000]
},{
"vendorID": [3300]
}]
}

how do i send the above list through url and how do parse the list in server end? I have tried sending as

xyz.zyz/search?vendorIDs={"vendorID":["111","1000"],"vendorID":["3300"]}

and parsing as

@RequestMapping(value="/search", method=RequestMethod.GET)
 public returntype myMethod(@RequestParam(value="vendorIDs") List<vendorID> vendorIDs) throws Exception{
//operation
return;
}

is it at all possible to send such list through url encoding? where am I going wrong about it?

xenteros
  • 14,275
  • 12
  • 47
  • 81
Burhan
  • 23
  • 4

1 Answers1

0

If you URL encode this: {"vendorID":["111","1000"],"vendorID":["3300"]} you will receive it in your controller but it will not be coerced into a List<VendorID> instead it will just be a JSON string. You could then use a JSON deserialiser to deserialise that into a List<VendorID>.

For example:

@RequestMapping(value="/search", method=RequestMethod.GET)
public returntype myMethod(@RequestParam(value="vendorIDs") String vendorIDs) throws Exception {
    List<VendorID> asList = objectMapper.readValue(vendorIDs, List.class);
    return;
}

Though at this point you might be wondering whether (a) it is best to POST this data and use Spring's built in JSON handling to present you with a List<VendorID> in your controller or (b) you can instruct Spring to apply its JSON handling to a RequestParam, in which case have a look at the accepted answer to this question.

glytching
  • 35,192
  • 8
  • 84
  • 96