An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments

前端 未结 2 1159
后悔当初
后悔当初 2020-12-09 16:24

I am teaching myself Spring by dissecting example applications and then adding code here and there to test theories that I develop during the dissection. I am getting the f

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 16:51

    Spring uses an interface called HandlerMethodArgumentResolver to resolve the parameter in your handler methods and construct an object to pass as an argument.

    If it doesn't find one, it passes null (I have to verify this).

    The BindingResult is a result object that holds errors that may have come up validating a @ModelAttribute, @Valid, @RequestBody or @RequestPart, so you can only use it with parameters that are annotated as such. There are HandlerMethodArgumentResolver for each of those annotations.

    EDIT (response to comment)

    Your example seems to show that the user should provide a pet type (as an integer). I would change the method to

    @RequestMapping(value = "/catowners", method = RequestMethod.GET)
    public String findOwnersOfPetType(@RequestParam("type") Integer typeID, Map model)
    

    And you would make your request (depending on your config) as

    localhost:8080/yourcontext/catowners?type=1
    

    Here too there is nothing to validate so you don't want or need a BindingResult. It would fail if you tried to add it anyway.

提交回复
热议问题