Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-7114

Allow Errors/BindingResult after @RequestBody

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 3.0.2
    • Fix Version/s: 3.2 M2
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Instead of using a command/form object, I bind much of my data to the request body. I expected that I could specify an Errors/BindingResult object after a @RequestBody method parameter. Currently, I do the following:

          @RequestMapping(value = "/events", method = RequestMethod.POST)
          public String create(@RequestBody final Event event,
                  final HttpServletResponse response, final Model model)
                  throws BindException {
              final BindingResult result = new BeanPropertyBindingResult(event, "");
              ValidationUtils.invokeValidator(this.eventValidator, event, result);
              if (result.hasErrors()) {
                  throw new BindException(result);
              }
              ...
          }
      

      I would like to do the following:

          @RequestMapping(value = "/events", method = RequestMethod.POST)
          public String create(@RequestBody final Event event,
                  final BindingResult result, final HttpServletResponse response,
                  final Model model) throws BindException {
              ValidationUtils.invokeValidator(this.eventValidator, event, result);
              if (result.hasErrors()) {
                  throw new BindException(result);
              }
              ...
          }
      

      However, when I do, I receive the following exception:

      2010-04-21 09:48:09,014 [http-8080-2] WARN org.springframework.web.servlet.handler.SimpleMappingExceptionResolver  - Handler execution resulted in exception
      org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String com.playonsports.event.controller.EventController.create(com.playonsports.event.bean.Event,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletResponse,org.springframework.ui.Model) throws org.springframework.validation.BindException]; nested exception is java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!
              at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
              at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
              at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
              at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
              at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
              ...
      Caused by: 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:264)
              at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163)
              ... 54 more
      

        Attachments

          Activity

            People

            Assignee:
            rstoya05-aop Rossen Stoyanchev
            Reporter:
            npadgett Nick Padgett
            Last updater:
            Spring Issues Spring Issues
            Votes:
            11 Vote for this issue
            Watchers:
            13 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 19 weeks, 1 day ago