5
votes

I'm getting na exception on the following method:

@Transactional
@RequestMapping(method=RequestMethod.PUT)
public @ResponseBody Account create(@Valid @RequestBody CreateAccountRequest request, BindingResult bindingResult)
{ ... 
}

Throws the following exception:

java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be immediately after the model attribute argument in the controller method signature: public com.mangofactory.concorde.domain.Account com.mangofactory.concorde.api.AccountService.create(com.mangofactory.concorde.api.rpc.CreateAccountRequest,org.springframework.validation.BindingResult)

According to the documentation, I'm required to add a BindingResult as the second parameter. However, I've done that.

It's even present in the exception.

What have I missed?

3

3 Answers

4
votes

Turns out that the way to solve this was to remove the BindingResult property completely.

The signature that worked was:

public @ResponseBody CreateAccountResponse create(@Valid @RequestBody CreateAccountRequest request)

This was as tipped by point #3 on this blog post.

3
votes

BindingResult is supported only after @ModelAttribute arguments. The combination of @Valid and @RequestBody raises a MethodArgumentNotValidException, which by default is translated to a 400 error code. This is documented in the reference documentation and on @RequestMapping itself.

0
votes

I don't think you need @RequestBody in there, as your CreateAccountRequest class should already specify which parameters are bound to which types/vars (thus negating the need to use the HttpMessageConverters Spring provides).