Annotation Type Required


  • @Documented
    @Constraint(validatedBy=RequiredValidator.class)
    @Target({METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE})
    @Retention(RUNTIME)
    public @interface Required
    Ensures that a value is provided (i.e. not null). Null values are always considered invalid. You can refine the behavior by allowing blank or empty Strings, collections, maps, or other types of object to be considered valid.

    For CharSequence objects (e.g. regular String objects), use allowBlank to allow empty or whitespace-only Strings. It does not make sense to set allowEmpty to true but leave allowBlank as false, so in general when annotating CharSequence objects, setting allowBlank to true is preferred, though it is also correct to set both allowBlank and allowEmpty to true.

    For Collection and Map objects, allowBlank has no effect, but allowEmpty will cause empty collections and maps to be considered valid.

    For any other type of object, only allowEmpty has any effect. If you have a custom object that has a public method named isEmpty that has no arguments and returns boolean, this validator will attempt to invoke that method reflectively and use its return value to determine whether the object is empty or not.

    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      boolean allowBlank
      Whether to allow an object that is a CharSequence to be blank, using StringUtils.isBlank(CharSequence) to perform the check.
      boolean allowEmpty
      Whether to allow an object to be "empty".
      Class<?>[] groups  
      String message  
      Class<? extends javax.validation.Payload>[] payload  
    • Element Detail

      • message

        String message
        Default:
        "{org.kiwiproject.validation.Required.message}"
      • groups

        Class<?>[] groups
        Default:
        {}
      • payload

        Class<? extends javax.validation.Payload>[] payload
        Default:
        {}
      • allowBlank

        boolean allowBlank
        Whether to allow an object that is a CharSequence to be blank, using StringUtils.isBlank(CharSequence) to perform the check.

        This only applies to CharSequence objects. All other (non null) objects are always considered as not blank.

        Returns:
        true to allow a CharSequence to be blank, false to consider blank CharSequence as invalid
        Default:
        false
      • allowEmpty

        boolean allowEmpty
        Whether to allow an object to be "empty". For String values, just checks the value itself. For anything else, attempts to find and call an isEmpty() method which allow this annotation to work on other types of objects, for example on Collection objects.
        Returns:
        true to allow an object to be empty, false otherwise
        Default:
        false