20

I'm having an issue with using @Valid on a parameter to a handler method on my @Controller. My code looks like this:

@RequestMapping(value=BIBBLE_BOBBLE_URI_PATTERN + "/widgets", method=RequestMethod.POST)
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public Widget saveNewWidget(
        @PathVariable final String username,
        @PathVariable final String bibbleBobbleName,
        @Valid @RequestBody final Widget widget,
        final BindingResult results,
        final HttpServletRequest request,
        final HttpServletResponse response)

where Widget is the class for one of my domain objects. I'm using the @RequestBody annotation to indicate that the payload of the request maps to widget (in my tests, the requests are JSON, though Jackson is on my classpath so I could also use XML).

As you can see, the BindingResult parameter follows directly after the Widget parameter, but I get the following error:

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

How do I apply the @Valid annotation to a @RequestBody parameter and then get the results?

P.S. I'm using annotation-driven to handle wiring up the controllers, do content-negotiation, etc.

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
Hank Gay
  • 70,339
  • 36
  • 160
  • 222
  • I'm learning all this still, **but**, try naming the attribute `@ModelAttribute("xyz") Widget widget`. – David Kerr Jun 07 '11 at 09:25
  • @DecaniBass will that address the problem of it not binding any data that I mentioned in my comment on @craftsman's answer? – Hank Gay Jun 07 '11 at 09:47
  • I know its a old POST, but is it working for you? I am using Spring 3.2. The binding works, but not @Valid. Question posted here [http://stackoverflow.com/q/16174990/668650] – Kevin Rave Apr 23 '13 at 17:01
  • remove parameter ```final BindingResult results,``` from controller method – Mr Lou Jul 31 '15 at 05:58
  • If you are using Kotlin with Spring Boot, then you must use `@field:Email`, `@field:NotNull` etc. on the respective fields to make sure that `@Valid` works. For information follow this: https://stackoverflow.com/questions/36515094/kotlin-and-valid-spring-annotation – Munish Chandel Oct 28 '18 at 03:11

2 Answers2

9

Are you using Spring 3.1? It is a newly added feature in Spring version 3.1. Please see Validation For @RequestBody Method Arguments

Ritesh
  • 7,472
  • 2
  • 39
  • 43
2

This is an issue in spring 3.0 and it is fixed in 3.1 M2.

https://jira.springsource.org/browse/SPR-6709

If you are using spring 3.0, based on some threads and extensive reading, here is the best approach for this problem. An aspect oriented approach so that we can back out when you upgrade it to the version of spring which fixes this issue.

An aspect which intercepts calls to method which uses @RequestMapping annotation and for each parameter which has @Valid annotation on it, call the corresponding validator in the aspect.

Chandra
  • 1,577
  • 3
  • 21
  • 28