Reputation: 973
I have a request hanlder (method in @Controller class) with some @RequestParam args invoked when a form is submitted. I have various types of args like strings, ints, enums and objects ... When the form is submitted if spring cannot bind a param to arg, returns a responce with error 400 or 500 (I haven't understood which cases correspond to each error yet). I would prefer to handle this mismatch by sending feedback to the user highlighting which field in the form was bad and why ...
One simple case of doing that is by changing all @RequestParams to strings, and do the conversions inside the handler and respond with feedbacks if any errors. (Errors may vary from type mismatch to invalid param ie negative number for age).
Is there any better way?
Upvotes: 3
Views: 2976
Reputation: 242696
@RequestParam
is most suited for situations when parameters are generated by your application rather than entered by the user.
If you want to provide feedback to the user it would be better to use conventional form handling approach with @ModelAttribute
and BindingResult
. This way you can perform arbitrary validation and also provide meaningful error messages for type mismatch errors.
Upvotes: 3