Class level validator not applicable to field in spring - java

I have a custom interface:
#Target({ TYPE, ANNOTATION_TYPE })
#Retention(RUNTIME)
#Constraint(validatedBy = { MyCustomValidator.class })
#Documented
public #interface ValidData {
String message() default EMPTY;
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
and I have a class that - until now - had this validator attached:
#ValidData(groups = AfterDefaultGroup.class)
public class RecoverData {
private String data;
It works, but I need to move the validator from class level to field level. I tried this:
public class RecoverData {
#ValidData(groups = AfterDefaultGroup.class)
private String data;
but I'm getting compilation error here:
ValidData not applicable to field
How can I fix it?

The #Target annotation defines where this annotation can be applied.
You are restricting it to TYPE and ANNOTATION_TYPEright now which doesn't allow to use it on fields.
According to the documentation you have to use ElementType.FIELD

Related

How to make a custom annotation inherit other annotations

I created a custom annotation, for a custom data type. Currently my entity looks like this:
#Entity
public class contact {
#Valid
#Randomizer(EmailAddressValidator.class)
#EmailAddressField
#Convert(converter = EmailAddressConverter.class)
#JsonSerialize(using = EmailAddressSerializer.class)
#Column(name = "email")
private EmailAddress email;
}
But I would like to have the following
#Entity
public class contact {
#EmailAddressField
#Column(name = "email")
private EmailAddress email;
}
What do I have to do in my own annotation, so that I don't have to write the other annotations for each property?
I would like to have everything together and only have to write one annotation to the property, for clarity.
My annotation currently looks like this:
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.FIELD, ElementType.TYPE_USE})
#Constraint(
validatedBy = {EmailAddressValidator.class}
)
#Documented
public #interface EmailAddressField {
Class<?>[] groups() default {};
String message() default "No Email Address";
Class<? extends Payload>[] payload() default {};
}
But if I change that to this it doesn't work:
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.FIELD, ElementType.TYPE_USE})
#Constraint(
validatedBy = {EmailAddressValidator.class}
)
#Documented
// -- Annotations from property
#Valid
#Randomizer(EmailAddressValidator.class)
#Convert(converter = EmailAddressConverter.class)
#JsonSerialize(using = EmailAddressSerializer.class)
#Inherited
// --
public #interface EmailAddressField {
Class<?>[] groups() default {};
String message() default "No Email Address";
Class<? extends Payload>[] payload() default {};
}
Java annotations are interfaces that can be applied to methods, classes, and fields and they are not capable of having other annotations as values because they are not objects, they are a form of metadata that don't have any methods or states and their main purpose is to provide metadata that can be read and processed by other tools such as the Java compiler or JVM.
A possible solution to avoid writing the following boilerplate codes would be creating a custom annotation processor that uses the AbstractProcessor class from the javax.annotation.processing package. This would allow you to define custom annotations and specify the actions that should be taken when they are encountered in your code during the compile time.
Here is the documentation; https://docs.oracle.com/en/java/javase/11/docs/api/java.compiler/javax/annotation/processing/Processor.html
https://docs.oracle.com/en/java/javase/11/docs/api/java.compiler/javax/annotation/processing/AbstractProcessor.html
Add following dependencies in the gradle , for configuruing #AutoService
dependencies {
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc5'
compileOnly 'com.google.auto.service:auto-service:1.0-rc5'
}
And here is the sample code;
#AutoService(Processor.class)
#SupportedAnnotationTypes("com.example.EmailAddressField")
#SupportedSourceVersion(SourceVersion.RELEASE_8)
public class EmailAddressFieldProcessor extends AbstractProcessor {
private Elements elementUtils;
#Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
this.elementUtils = processingEnv.getElementUtils();
}
#Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) {
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
if (element.getKind() == ElementKind.FIELD) {
VariableElement field = (VariableElement) element;
AnnotationMirror validAnnotation = elementUtils.getAnnotationMirror(Valid.class);
field.getAnnotationMirrors().add(validAnnotation);
AnnotationMirror convertAnnotation = elementUtils.getAnnotationMirror(Convert.class);
field.getAnnotationMirrors().add(convertAnnotation);
// BlahBlah.class annotation..... etc...
}
}
}
return true;
}
}

Variable into Spring Boot class annotation

Is there a way to pass a variable from a property file to a class annotation?
#MaxPeriod(firstDateField = "startDateFrom", secondDateField = "startDateTo", maxPeriod = "${variable.from.property:31}")
public class ProcessReportFilter {
private LocalDate startDateFrom;
private LocalDate startDateTo;
}
upd: annotation is used in the request object at bean validation
#Target({ElementType.TYPE})
#Retention(RUNTIME)
#Constraint(validatedBy = MaxPeriodValidator.class)
#Repeatable(MaxPeriod.List.class)
public #interface MaxPeriod {
String message() default "Period between {firstDateField} and {secondDateField} must not exceed {maxPeriod} day(-s)";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String firstDateField();
String secondDateField();
long maxPeriod();
#Target({ElementType.TYPE})
#Retention(RUNTIME)
#interface List {
MaxPeriod[] value();
}
}

How to interpolate message dynamically in ConstraintValidator with property node?

Assume I have simple dto with one field and this dto is also annotated with custom validation annotation:
#CustomAnnotation
public class SimpleDto {
private String field;
}
// setters and getters omited
Custom annotation:
#Target(TYPE)
#Retention(RUNTIME)
#Constraint(validatedBy = CustomValidator.class)
#Documented
public #interface CheckMondialRelayShopOrderWeight {
String message() default "{temp.key.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
and finally validator itself:
public class CustomValidator implements
ConstraintValidator<CustomAnnotation, SimpleDto> {
#Override
public void initialize(CustomAnnotation constraintAnnotation) {}
#Override
public boolean isValid(SimpleDto value,
ConstraintValidatorContext context) {
HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);
hibernateContext.addMessageParameter("dynamicValue", 130);
hibernateContext.buildConstraintViolationWithTemplate(hibernateContext.getDefaultConstraintMessageTemplate()).addPropertyNode("field").addConstraintViolation().disableDefaultConstraintViolation();
return false;
}
}
and in application.properties:
CustomAnnotation.simpleDto.field=Your dynamic value is {dynamicValue}
But this doesn't work, this works just fine if I put hibernateContext.buildConstraintViolationWithTemplate("Your dynamic value is {dynamicValue}").addConstraintViolation().disableDefaultConstraintViolation(); instead of hibernateContext.buildConstraintViolationWithTemplate(hibernateContext.getDefaultConstraintMessageTemplate()).addPropertyNode("field").addConstraintViolation().disableDefaultConstraintViolation();
I can't figure out how to use the interpolation with addPropertyNode. Any suggestions?

Dropwizard custom validation annotation not working

I'm trying to apply multiple common annotations at once with a custom validation annotation like this:
#Retention(RetentionPolicy.RUNTIME)
#Length(max=25, min=1, message="invalid length")
#NotNull
#Pattern(regexp = "[a-zA-Z0-9]{1, 25})")
public #interface MyAnnotation {
}
And using it in my model classes like this:
#MyAnnotation
public String firstName;
None of these validation are working, but they work as expected when used in the model class itself. I also tried registering MyAnnotation in the applications run method, and that didn't work either.
environment.jersey().register(MyAnnotation.class);
What else do I need to do in order to use custom validations?
Either annotate the String directly:
#Pattern(regexp = "[a-zA-Z0-9]{1, 25})")
#NotNull
#Length(max=25, min=1, message="invalid length")
public String firstName;
Or create a validator, something like:
class MyAnnotatationValidator implements ConstraintValidator<MyAnnotation, String>{
#Override
public void initialize(MyAnnotation a){}
#Override
public boolean isValid(String s, ConstraintValidationContext c) {
return s != null && (s.length() > 0 && s.length() < 26) && s.matches("[a-zA-Z0-9]{1, 25})";
}
}
And have
#Retention(RetentionPolicy.RUNTIME)
#Constraint(validatedBy=MyAnnotatationValidator.class)
public #interface MyAnnotation {
String message() default = "{MyAnnotation}";
Class<?>[] groups() default {};
Class<? extends Payload> payload() deafult {};
}
According to the JSR-303
A constraint definition may have attributes that are specified at the time the constraint is applied to a JavaBean. The properties are mapped as annotation elements. The annotation element names message, groups and payload are considered reserved names; annotation elements starting with valid are not allowed; a constraint may use any other element name for its attributes.
So you must add message, groups and payload attributes to your #MyAnnotation.
A composition is done by annotating the composed constraint, example:
#Length(max=25, min=1, message="invalid length")
#NotNull
#Pattern(regexp = "[a-zA-Z0-9]{1, 25})")
#Documented
#Target({ANNOTATION_TYPE, METHOD, FIELD, CONSTRUCTOR, PARAMETER})
#Retention(RUNTIME)
public #interface MyAnnotation {
String message() default "My annotation message";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

Java annotation for Null but neither Empty nor Blank

Is there are any java annotation(s) that can validate like the example below?
String test;
test = null; //valid
test = ""; //invalid
test = " "; //invalid
test = "Some values"; //valid
You need to create a custom annotation: #NullOrNotBlank
First create the custom annotation: NullOrNotBlank.java
#Target( {ElementType.FIELD})
#Retention(RUNTIME)
#Documented
#Constraint(validatedBy = NullOrNotBlankValidator.class)
public #interface NullOrNotBlank {
String message() default "{javax.validation.constraints.NullOrNotBlank.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default {};
}
Then the actual validator: NullOrNotBlankValidator.java
public class NullOrNotBlankValidator implements ConstraintValidator<NullOrNotBlank, String> {
public void initialize(NullOrNotBlank parameters) {
// Nothing to do here
}
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
return value == null || value.trim().length() > 0;
}
}
There isn't such an annotation in either javax.validation or Hibernate Validator. There was a request to add one to Hibernate Validator but it was closed as "won't fix" due to the possibility of writing your own relatively easily. The suggest solution was to either use your own annotation type defined like this:
#ConstraintComposition(OR)
#Null
#NotBlank
#ReportAsSingleViolation
#Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
#Retention(RUNTIME)
#Constraint(validatedBy = { })
public #interface NullOrNotBlank {
String message() default "{org.hibernate.validator.constraints.NullOrNotBlank.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
or to use the #Pattern annotation with a regular expression that requires a non-whitespace character to be present (as the Pattern annotation accepts nulls and does not match them against the pattern).
Where is a nice javax.validation.constraints.Pattern annotation.
You can annotate the field with:
#Pattern(regexp = "^(?!\\s*$).+", message = "must not be blank")
This checks if field matches regex. The regex itself is something but not blank (see details here). It uses negative lookahead.
This is possible without creating a custom annotation, by using javax.validation.constraints.Size
// Null values are considered valid
#Size(min=1) String test;
The best way is to create your own constraint validator,
//custom annotation
#Documented
#Constraint(validatedBy = CustomCheck.class)
#Target( { ElementType.METHOD, ElementType.FIELD })
#Retention(RetentionPolicy.RUNTIME)
public #interface CustomConstarint {
String message() default "Invalid data";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
//validation logic goes here
public class CustomCheck implements
ConstraintValidator<CustomConstarint, String> {
#Override
public void initialize(CustomConstarint customConstarint) {
}
#Override
public boolean isValid(String field,
ConstraintValidatorContext cxt) {
//write your logic to validate the field
}
}
Did you try Hibernate-Validator? I think that's what you are looking for.
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
public class MyModel {
#NotNull
private String str1;
#NotEmpty
private String str2;
#NotBlank
private String str3;
}

Categories