4

I have an end point for getting all objects from a given table/data_type. I want to add some filtering capabilities on the data returned but am unable to figure out how to pass a JSON object to my controller.

my code:

    @RequestMapping(value = "/{dataType}.json", method = RequestMethod.GET)
public @ResponseBody List findAll(@PathVariable String dataType, @RequestParam(required=false) Map<String, Object> query) {
}

How can I pass data to query parameter? I tried @ModelAttribute and sent the JSON object in request body but it did not work.

Please help me figure this out

user1221612
  • 465
  • 1
  • 7
  • 18

1 Answers1

3

You can't send JSON on the request parameter, directly. From the docs:

When an @RequestParam annotation is used on a Map or MultiValueMap argument, the map is populated with all request parameters.

I'm pretty sure you'll need to do something like call encodeURIComponent() on the json structure you want to pass to your server and then have the argument just be a string. On the server side you can use jersey or something to convert the string back into something you can manipulate.

This post may provide some more insight:

Spring MVC: Complex object as GET @RequestParam

Community
  • 1
  • 1
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86