Spring MVC 3 exception: Errors / BindingResult argument expected

I get an exception with 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: the Errors / BindingResult argument is expected immediately after the model attribute argument in the controller 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 have to add BindingResult as the second parameter. However, I did it.

He is even present in the exception.

What did I miss?

+4
source share
3 answers

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

Signature Signed:

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

This was as stated in paragraph 3 on this blog post.

+4
source

BindingResult is only supported after the @ModelAttribute arguments. The combination of @Valid and @RequestBody calls the MethodArgumentNotValidException method, which by default translates into error code 400. This is described in the help documentation and directly on @RequestMapping.

+3
source

I don’t think you need @RequestBody there, since your CreateAccountRequest class should already indicate which parameters are bound to which types / vars (thereby denying the need to use HttpMessageConverters Spring).

0
source

Source: https://habr.com/ru/post/1394823/


All Articles