A brief introduction to Bean Validation

The Bean Validation framework allows you to specify the validation rules in the form of annotations on a field, method, or a Java class. You can either use built-in constraints offered by Bean Validation or build custom constraints by using the extension mechanisms offered by the framework. The following table summarizes the built-in constraints offered by Bean Validation that are applicable to all data types:

Validation constraints

Supported Java types

Details of the imposed constraints

@NotNull

Applicable to all Java types

The annotated variable must not be null.

@Null

Applicable to all Java types

The annotated variable must be null.

 

The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the Boolean data types:

Validation constraints

Supported Java types

Details of the imposed constraints

@AssertFalse

  • java.lang.Boolean
  • Boolean

The annotated variable must be false.

@AssertTrue

  • java.lang.Boolean
  • Boolean

The annotated variable must be true.

The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the number data types:

Validation constraints

Supported Java types

Details of the imposed constraints

@DecimalMax

  • java.math.BigDecimal
  • java.math.BigInteger
  • java.lang.String
  • byte, short, int, long, and their wrapper types

The annotated variable must not exceed the maximum specified limit.

@DecimalMin

  • java.math.BigDecimal
  • java.math.BigInteger
  • java.lang.String
  • byte, short, int, long, and their wrapper types

The annotated variable must be higher than or equal to the minimum specified value.

@Digits

  • java.math.BigDecimal
  • java.math.BigInteger
  • java.lang.String
  • byte, short, int, long, and their wrapper types

The annotated variable must be a number within the acceptable range.

@Max

  • java.math.BigDecimal
  • java.math.BigInteger
  • byte, short, int, long, and their wrapper types

The annotated variable must be a number with a value less than or equal to the specified maximum value.

@Min

  • java.math.BigDecimal
  • java.math.BigInteger
  • byte, short, int, long, and their wrapper types

The annotated variable must be a number with a value larger than or equal to the specified minimum value.

The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the date data types:

Validation constraints

Supported Java types

Details of the imposed constraints

@Future

  • java.util.Date
  • java.util.Calendar

The annotated variable must be a date in the future.

@Past

  • java.util.Date
  • java.util.Calendar

The annotated variable must be a date in the past.

 

The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the string data types:

Validation constraints

Supported Java types

Details of the imposed constraints

@Pattern

java.lang.String

The annotated string must match the regular expression specified.

 

The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the collection and string data types:

Validation constraints

Supported Java types

Details of the imposed constraints

@Size

  • java.lang.String (string length is evaluated)
  • jav.util.Collection (collection size is evaluated)
  • java.util.Map (map size is evaluated)
  • * java.lang.Array (array length is evaluated)

The annotated string or collection size must be between the specified boundaries (included).

The following code snippet illustrates the use of built-in validation constraints provided by Bean Validation for validating an input to a JAX-RS method call. This example validates the input parameter for a positive number:

@GET 
@Path("departments/{id}") 
@Produces(MediaType.APPLICATION_JSON) 
public Department findDepartment(@PathParam("id") 
    @Min(value = 0, message = "Department Id must be a  
    positive value")  
    Short id, @Context Request request) { 
 
    Department department = findDepartmentEntity(id); 
    return department; 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.205.99