Skip to content

JSR-303 @Valid validation is not invoked for @RequestBody controller method arguments [SPR-6709] #11375

Closed

Description

@spring-projects-issues

Scott Frederick opened SPR-6709 and commented

If an argument to a Spring MVC Controller method is annotated with both the @RequestBody and @Valid annotations, the argument should be passed to the validation framework after being populated, but the validation is not invoked.

More details and an example are included in the forum thread.


Affects: 3.0 GA

Reference URL: http://forum.springsource.org/showthread.php?t=81563

Referenced from: commits f1ad53d, 9d2ee70, 18f5d90

26 votes, 30 watchers

Activity

  1. spring-projects-issues commented on Feb 3, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Michal Huniewicz commented

    Yep, I confirm this. I have the same problem.

  2. spring-projects-issues commented on May 18, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Arjen Poutsma commented

    Rossen, could you take a look at this one?

  3. spring-projects-issues commented on Jul 20, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Vijay Dendukuri commented

    I'm using 3.1 M2, But i'm still getting the following exception.

    java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!
    org.springframework.web.method.annotation.support.ErrorsMethodArgumentResolver.resolveArgument(ErrorsMethodArgumentResolver.java:60)
    org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:65)
    org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:153)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:502)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:465)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:863)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)

  4. spring-projects-issues commented on Jul 20, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Vijay Dendukuri commented

    I take it back, Actually its working now. With out the BindingResult Argument

  5. spring-projects-issues commented on Jan 7, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Alexander Shabanov commented

    The issue still reproduced on 3.1.0.RELEASE

  6. spring-projects-issues commented on Jan 9, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Is it possible that you're using the AnnotationMethodHandlerAdapter still? New features in annotated controllers require switching to the support classes listed here.

  7. spring-projects-issues commented on Jan 10, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Alexander Shabanov commented

    Yes, I think this is the reason. I didn't try it yet, but I'm pretty sure this would solve the problem. Please, close the bug.

  8. spring-projects-issues commented on Jan 10, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Resolving as requested.

  9. spring-projects-issues commented on Jan 25, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Mike Finney commented

    I also got a java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!

    I am using Spring 3.1.0.RELEASE. Other than using the 3.1.0 jars, how can I make sure I am using the latest "support classes" listed here: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/new-in-3.1.html#d0e1515 Is there a xml declaration or java config I should do?

  10. spring-projects-issues commented on Jan 25, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    See comment by Vijay above. You need to remove the BindingResult or Errors method argument. That can only be used after a @ModelAttribute argument.

  11. spring-projects-issues commented on Feb 16, 2012

    @spring-projects-issues
    CollaboratorAuthor

    chris fang commented

    @Valid @RequestBody can work together in 3.1 , but how can we get the errors without the BindingResult method argument?

  12. spring-projects-issues commented on Feb 16, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    The idea here is that @RequestBody validation errors can be handled in one place. The DefaultHandlerExceptionResolver handles it by returning a 400 error. You can do the same if you want to add more detail to the body of the response.

  13. spring-projects-issues commented on Feb 16, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Scott Frederick commented

    @Chris - Rossen's answer is, of course, the best technique provided you want validation errors to be handled in the same way across your application (i.e. you want the body of the response to be the same). If you need to customize validation error handling per controller, another option is to add a @ExceptionHandler-annotated method to the controller, passing "org.springframework.validation.BindException.class" as the value parameter to the annotation (see http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-exceptionhandler). The BindException has a getBindingResult() method that gives the details of the validatation failure.

  14. spring-projects-issues commented on Feb 16, 2012

    @spring-projects-issues
    CollaboratorAuthor

    chris fang commented

    @Scott Frederick - Thanks for your response. I tried @ExceptionHandler(BindException.class). But @Valid doesn't throw any bindException when using with @RequestBody. I think I may need to switching to the new support class in order to get the new feature ( RequestMappingHandlerMapping
    RequestMappingHandlerAdapter
    ExceptionHandlerExceptionResolver)

    Thanks a lot.

  15. spring-projects-issues commented on Feb 16, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    I think Scott meant MethodArgumentNotValidException. This is described in the documentation and in the javadoc of @RequestMapping.

  16. 5 remaining items

  17. spring-projects-issues commented on Mar 22, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Thomas Bruyelle commented

    Rajeev,

    I use @Valid and @RequestBody with Spring 3.1.1.RELEASE and it works like a charm.

    Like Scott said, your problem is about the BindingResult parameter. I suggest you don't use the BindingResult in parameter of your controller method, but rather declare a new method annotated with @ExceptionHandler on MethodArgumentNotValidException in your controller. This method will be invoked each time the validation failed.

    Exception MethodArgumentNotValidException gives you access to the BindingResult :

    @ExceptionHandler
    public void handleMethodArgumentNotValidException( MethodArgumentNotValidException error )
    {
        (...) error.getBindingResult() (...)
    }
  18. spring-projects-issues commented on Mar 23, 2012

    @spring-projects-issues
    CollaboratorAuthor

    rajeev singla commented

    Thanks for response Scott and Thomas.

  19. spring-projects-issues commented on Mar 26, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Puneet Pandey commented

    Scott and Thomas: I use this combination of @Valid and @RequestBody with Spring 3.1.1 and it works perfectly. However, my requirement is such that I need to use Spring 3.1.0. I use Spring 3.1.0 and deploy the code to a Jetty server and while deploying itself I run into error. (Very same code with Spring 3.1.1 gets deployed and runs like gem).

    The issue I get while deploying my code is : Failed startup of context o.m.j.p.JettyWebAppContext{/,file:/D:/SAMS-2013/WORKSPACE-06/com.sams.validation/src/main/webapp/},file:/D:/SAMS-2013/WORKSPACE-06/com.sams.validation/src/main/webapp/
    java.util.zip.ZipException: invalid entry size (expected 3100 but got 2760 bytes).

    Is it that the combination of @Valid and @RequestBody does not work? Please suggest.

  20. spring-projects-issues commented on Mar 26, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Puneet Pandey commented

    Is it that the combination of @Valid and @RequestBody does not work with Spring 3.1.0?

  21. spring-projects-issues commented on Mar 26, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Scott Frederick commented

    I don't see anything to indicate that the combination of @Valid and @RequestBody is the source of the ZipException, but there isn't very much information here to go on. I suggest posting your problem, with as much detail as possible, to the Web section of the Spring Forum: http://forum.springsource.org/forumdisplay.php?25-Web.

  22. spring-projects-issues commented on Mar 27, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Puneet Pandey commented

    Scott: I too couldn't see any relation between Validation annotations and ZipException. Unfortunately, however, I was getting the exception only when I was changing the Spring version. I could process a little further but couldn't get the issue resolved completely. Will post the issue in Springsource forum too. Thanks!

  23. spring-projects-issues commented on Mar 27, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Drotbohm commented

    An exception like this usually indicates corrupted JAR files. Have you tried purging your local Maven repo (or equaivalent) and re-grabbing the binaries?

  24. spring-projects-issues commented on Mar 27, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Puneet Pandey commented

    Oliver: You are correct, it was actually a case of corrupted jar. I have actually progressed and deployed the code to the Virgo Server. But, still could not get it to work. Have posted the issue to the SpringSource. Please have a look into it and suggest.

    Link:

    http://forum.springsource.org/showthread.php?135923-Spring3-MVC-annotation-driven-Rest-Services-in-OSGi-environment-bean-validation-issue&p=441701#post441701

  25. added
    type: enhancement
    has: votes-jira
    in: web
    on Jan 11, 2019
  26. added this to the 3.1 M2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

has: votes-jirain: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions