Sending json object to GET method in spring MVC

I have an endpoint for retrieving all objects from a given table / data _type. I want to add some filtering capabilities for the returned data, but I cannot figure out how to pass the JSON object to my controller.

my code is:

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

How to pass data to the request parameter? I tried @ModelAttribute and sent a JSON object to the request body, but that didn't work.

Please help me figure this out.

+4
source share
1 answer

You cannot send JSON directly by request parameter. From the docs:

When the @RequestParam annotation is used on the map or the MultiValueMap argument, the map is populated with all the query parameters.

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

This post may provide further information:

Spring MVC: complex object like GET @RequestParam

+2
source

All Articles