Sometimes Spring MVC will amaze you with totally unexpected exceptions. You have no idea why that is coming. For instance, recently I wrote a small piece of Spring MVC Controller code, one like below:
@Controller
public class UserController {
@RequestMapping(value = "addUser")
public String addUser(@ModelAttribute("userForm") UserForm userForm,
ModelMap map, BindingResult results) {
if (results.hasErrors()) {
return "add_user_form";
}
return "add_user_success";
}
//...
}
Code language: Java (java)
And while executing the application, I got a strange exception:
Throwable occurred: java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:335)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
Code language: Java (java)
What!!!?
Problem Statement
How to solve below exception in Spring MVC?
java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature.
Solution
Well basically Spring expects the BindingResult
attribute to be present right after your Model attribute. So if we change the above controller method to:
@RequestMapping(value = "addUser")
public String addUser(@ModelAttribute("userForm") UserForm userForm,
BindingResult results, ModelMap map)
Code language: Java (java)
This will solve the error. Just keep in mind. Whenever you want to define BindingResult
object in your controller method, declare it right after the (@ModelAttribute
) model attribute.
Spring’s documentation says: The Errors
or BindingResult
parameters have to follow the model object that is being bound immediately as the method signature might have more that one model object and Spring will create a separate BindingResult instance for each of them so the following sample won’t work: Hope this is useful.
I had the same issue too! But I’ve read your blog after finding this by myself. But good article!
sry.same error occurs .
I too was having the same error. Thanks this post help me understand the error.
its working….thanks
Excellent, Thanks very much for this blog. I had the same issue wasting my time and then ended up on this blog and solution worked like a magic…. Thanks again
Very very useful…
Thanks for the post
ResponseEntity submitPetData(@RequestHeader(value = “Authorization”, required = true) String token, @Valid @PathVariable(value = “countryCode”, required = true) String countryCode,
BindingResult bindingResult, Principal principal) {
For my example I dont have modelAttribute.. I am using @PathVariable and geting the same error
Caused by: java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply: public org.springframework.http.ResponseEntity