Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 3 exception : An Errors/BindingResult argument is expected

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?

like image 409
Marty Pitt Avatar asked Feb 05 '12 01:02

Marty Pitt


2 Answers

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.

like image 116
Marty Pitt Avatar answered Nov 15 '22 00:11

Marty Pitt


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.

like image 41
Rossen Stoyanchev Avatar answered Nov 14 '22 23:11

Rossen Stoyanchev