Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSR-303 Annotating Field vs Getter

When using JSR-303 annotations to perform bean validation, what is the difference between annotating the field versus the getter?

Is one approach recommended over the other?

Annotation on field

public class Person {

  @NotBlank
  private String firstName;

  public String getFirstName() {
    return firstName;
  }

  public String setFirstName(String firstName) {
    this.firstName = firstName;
  }
}

Annotation on getter

public class Person {

  private String firstName;

  @NotBlank
  public String getFirstName() {
    return firstName;
  }

  public String setFirstName(String firstName) {
    this.firstName = firstName;
  }
}
like image 409
kernelpanic Avatar asked Jan 10 '18 04:01

kernelpanic


People also ask

What is JSR 303 annotations?

The objective of the JSR-303 standard is to use annotations directly in a Java bean class. JSR 303 specification allows the validation rules to be specified directly into the fields inside any Java class which they are intended to validate, instead of creating validation rules in separate classes.

In which implementation is the JSR 303 standard used?

Spring Framework became JSR303 compliant from version 3 onward I think. Spring Framework 4.0 supports Bean Validation 1.0 (JSR-303) and Bean Validation 1.1 (JSR-349) in terms of setup support, also adapting it to Spring's Validator interface.

What is the purpose of custom validator annotation?

A custom validation annotation can also be defined at the class level to validate more than one attribute of the class. A common use case for this scenario is verifying if two fields of a class have matching values.

How does bean validation work?

Very basically, Bean Validation works by defining constraints to the fields of a class by annotating them with certain annotations.


1 Answers

Constraint declarations are placed on classes or interfaces primarily through annotations. A constraint annotation (see Section 2.1, “Constraint annotation”), can be applied to a type, on any of the type's fields or on any of the JavaBeans-compliant properties.

When a constraint is defined on a class, the class instance being validated is passed to the ConstraintValidator. When a constraint is defined on a field, the value of the field is passed to the ConstraintValidator. When a constraint is defined on a getter, the result of the getter invocation is passed to the ConstraintValidator.

The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type's public API that way. They will even be added to the generated JavaDoc. A user of a type knows that way which constraints apply to it without looking into its internal implementation.

Another advantage of annotating getters is that constraints can be put at methods on base classes or interfaces and also apply for any sub-types/implementations.

like image 56
VPK Avatar answered Oct 11 '22 05:10

VPK