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
If you have a parameter of type BindingResult it is essentially to hold any errors when binding http request parameters to a variable declared directly preceding the BindingResult method parameter.
So all of these is acceptable:
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@Valid MyType type, BindingResult result, ...)
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@ModelAttribute MyType type, BindingResult result, ...)
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@RequestBody @Valid MyType type, BindingResult result, ...)
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<String, Object> 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.