Nan
Nan

Reputation: 339

springmvc handle the json request

I understand if I use springMVC and pass a json object to the controller, it will try to bind the json object to the controller pararmenter, but how to handle the binding error? I use something like this but seems not userful.

public String save(@RequestBody @Valid SomeList list, BindingResult result){
    if(result.hasError()){
        System.out.println(result);
        }
}

Upvotes: 0

Views: 514

Answers (2)

Bozho
Bozho

Reputation: 597106

Generally, you can return the same view that submitted the data. If you have <form:error> tags there, they will be displayed (because of the binding information).

But this is most certainly an ajax call, so what you can do is set a specific response status in the if body:

response.setStatus(HttpServletResponse.NOT_ACCEPTABLE);

and then look for that status code (406) in the ajax response handler. If you want precise validation information, you can try serializing the binding result itself as a response.

Upvotes: 1

dardo
dardo

Reputation: 4960

System.out.println will do next to nothing. Basically what it's saying is output the binding result to the server jvm std out.

Since you're returning a String, I'm going to assume you're returning a view name, so you might want to redirect the user to an error page.

Upvotes: 0

Related Questions