Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

400 Bad request when validation Model Attribute with Spring MVC

I am writing a simple controller that accepts request on user sign-up I want to validate the Model Attribute but I receive 400 bad request. I've checked this question here and is pretty much the problem I have but the solutions does not work for me.

 @RequestMapping(method = RequestMethod.POST, value = "/sign-up", consumes = "application/x-www-form-urlencoded")
    public ModelAndView addUser(ModelMap model,@Valid @ModelAttribute
    UserRegistrationInfo userRegistrationInfo,
                                HttpServletRequest httpRequest,
                                HttpSession httpSession,
                                BindingResult result) { ..... } 

EDIT

Spring Version: 4.0.6.RELEASE

I've read SpringMVC architecture and set a break points at DefaultHandlerExceptionResolverclass, doResolveException method and found that a BindException exception is thrown.However, I did not know why BindingResult is not filled and method execution is not called to let me determine what behavior I want ? the execution ends up at return handleBindException((BindException) ex, request, response, handler); which is

protected ModelAndView handleBindException(BindException ex, HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return new ModelAndView(); } 
like image 328
Adelin Avatar asked Nov 30 '22 00:11

Adelin


1 Answers

As you've rightly figure out, you're getting a bad request on account of the binding errors. All the info that you need are in fact contained in the BindingResult object but in order to use it properly you should set your BindingResult to immediately follow your ModelAttribute e.g.

 @RequestMapping(method = RequestMethod.POST, value = "/sign-up", consumes = "application/x-www-form-urlencoded")
    public ModelAndView addUser(ModelMap model,@Valid @ModelAttribute
    UserRegistrationInfo userRegistrationInfo, BindingResult result
                                HttpServletRequest httpRequest,
                                HttpSession httpSession,
                                ) { ..... }

While ordering of arguments generally doesn't matter, BindingResult parameter is an exception, as the method can contain several ModelAttribute parameters, each having its own dedicated instance of BindingResult. In this case the association is established by having the BindingResult parameter immediately follow the ModelAttribute it applies to.

When you reorder your parameters you will no longer get the 400 error, rather the request will enter the controller, and the logs will show the exact binding problem, or you simply check if result.hasErrors() and iterate through field errors by calling result.getAllErrors(). Then it should be simple enough to resolve your binding issue and the consequent bad request.

Check the section od the doc http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-methods

like image 73
Master Slave Avatar answered Dec 02 '22 15:12

Master Slave