Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC & JSR 303 validation leads to error 400

OK. I have a Spring MVC controller method as below:

public String getInformationByGID(@ModelAttribute("geoInfoParam") @Valid GeoInfoParam geoInfoParam, Model model ,BindingResult result)

GeoInfoParam class defines has a number of inputs with @NotNull, @Size annotated validations, which I expect to be executed when I submit form (as I have @Valid with ModelAttribute). But what happened, When I click submit, page showed me 400 error and did not show anyother information in log.

like image 530
bkrish Avatar asked Aug 05 '14 18:08

bkrish


1 Answers

I debugged the execution flow and found that org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.java ( which is part HandlerAdapter execution).handleBindException is called when there is no BindingResult followed by ModelAttribute and this method sets response with 400 code. The lesson I learned is If you have a form(ModelAttribute) annotated with @Valid, make sure it is followed by BindingResult immediately. So I have changed my controller method signature as below:

public String getInformationByGID(@ModelAttribute("geoInfoParam") @Valid GeoInfoParam geoInfoParam, BindingResult result, Model model)

Silly Me. Now everthing is fine. Hope this helps someone.

NB: I use Spring MVC 3.2.8 and Hibernate validator 4.2.0 final

like image 170
bkrish Avatar answered Oct 10 '22 23:10

bkrish