I have a rest service with my request body bean annotated with javax.validation like @NotBlank @NotNull @Pattern
etc., and in one specific field I receive a file encoded as a string base64,
so, is there an annotation, or how could I write a custom validation annotation, so it would check if the string is really a base64 string?
I just need a validation like this in annotation form:
try {
Base64.getDecoder().decode(someString);
return true;
} catch(IllegalArgumentException iae) {
return false;
}
thnx in advance
Yes, you could write your own annotations and validators for them.
Your annotation would look like this:
Constraint validator
javax.validation
implementation (I'm using here your code for the actual validation):Example data class with the annotated field:
And controller method example with
@Valid
annotation which is required:UPDATED: Also, if you want to check whether the fiven string is a base64 string, you can use the method
isBase64()
from apache-commons libraby, the class isorg.apache.commons.codec.binary.Base64
So, it would look like thisBase64.isBase64(str);