Class Validation

java.lang.Object
jakarta.validation.Validation

public class Validation extends Object
This class is the entry point for Jakarta Validation.

There are three ways to bootstrap it:

  • The easiest approach is to build the default ValidatorFactory.
     ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
     
    In this case, the default validation provider resolver will be used to locate available providers.

    The chosen provider is defined as followed:

    • if the XML configuration defines a provider, this provider is used
    • if the XML configuration does not define a provider or if no XML configuration is present the first provider returned by the ValidationProviderResolver instance is used.
  • The second bootstrap approach allows to choose a custom ValidationProviderResolver. The chosen ValidationProvider is then determined in the same way as in the default bootstrapping case (see above).
     Configuration<?> configuration = Validation
        .byDefaultProvider()
        .providerResolver( new MyResolverStrategy() )
        .configure();
     ValidatorFactory factory = configuration.buildValidatorFactory();
     
  • The third approach allows you to specify explicitly and in a type safe fashion the expected provider.

    Optionally you can choose a custom ValidationProviderResolver.

     ACMEConfiguration configuration = Validation
        .byProvider(ACMEProvider.class)
        .providerResolver( new MyResolverStrategy() )  // optionally set the provider resolver
        .configure();
     ValidatorFactory factory = configuration.buildValidatorFactory();
     

Note:

  • The ValidatorFactory object built by the bootstrap process should be cached and shared amongst Validator consumers.
  • This class is thread-safe.