I am using spring boot to run an API. I want to validate the user request parameters before deserialization to prevent jackson from throwing deserialization exceptions. My thinking is it's better to validate without exception handling but if you think I'm wrong please let me know.
Right now I made an exception handler controller with an @ControllerAdvice annotation to catch exceptions before they bubble up to the user but is there a way to validate user input before jackson deserialization to check for errors?
I started playing around with spring validation but if I write a custom validator and annotate the request parameters with @Validate does the validation take place before deserialization? Is spring validation the way to go?
I looked into validation with annotations (JSR 380) but I think I need more control over validation so that's why I'm writing my own custom validator.
I was thinking I could write a custom deserializer that calls my custom validator but is that the best way to do this. I'm looking for best practices and a good strategy for validation.
Thanks in advance for your help.
Spring Validation is an examination of Bean state. Bean class has annotations and they put some constraints on bean field values.
So in order to invoke the "validation process" the bean should already exist. Creation of bean out of string json/xml representation that comes along with request is exactly a deserialization process.
Hence the answer is that spring first deserializes the object by using some library (like jackson for example) and only then tries to validate an input by applying validation techniques.