Parsing json into java objects in spring-mvc

Viewed 63294

I'm familiar with how to return json from my @Controller methods using the @ResponseBody annotation.

Now I'm trying to read some json arguments into my controller, but haven't had luck so far. Here's my controller's signature:

@RequestMapping(value = "/ajax/search/sync")
public ModelAndView sync(@RequestParam("json") @RequestBody SearchRequest json) {

But when I try to invoke this method, spring complains that: Failed to convert value of type 'java.lang.String' to required type 'com.foo.SearchRequest'

Removing the @RequestBody annotation doesn't seem to make a difference.

Manually parsing the json works, so Jackson must be in the classpath:

// This works
@RequestMapping(value = "/ajax/search/sync")
public ModelAndView sync(@RequestParam("json") String json) {
    SearchRequest request;
    try {
        request = objectMapper.readValue(json, SearchRequest.class);
    } catch (IOException e) {
        throw new IllegalArgumentException("Couldn't parse json into a search request", e);
    }

Any ideas? Am I trying to do something that's not supported?

2 Answers
Related