Spring 3.1 JSON data binding

441 views Asked by At

I am using JSON binding as following in my controller

public @ResponseBody Person getPerson(@RequestBody PersonReq req){
    // Some code here..
    ....
}

If for some reason the JSON request sent is invalid, then spring throws a very generic error. Will I be able to gracefully handle this error?

What I want to do is something similar to form databinding where you can use BindingResult to capture the errors. (as shown below)

public String getPersonHtml(PersonReq req, BindingResult result){
    if (result.hasErrors()){
        // do error handling here..
    }
}

I tried similar approach by providing additional argument (BindingResult) but that didnt help.

1

There are 1 answers

2
Har On

Use Fiddler to confirm that the JSON request is valid.