Annotation Interface ValidURL


@Documented @Constraint(validatedBy={}) @Target({METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE}) @Retention(RUNTIME) public @interface ValidURL
Validates that a CharSequence represents a valid URL.

This annotation can be used to validate that a string field, parameter, etc. contains a valid URL. By default, only HTTP and HTTPS schemes are allowed, but this can be configured using the allowAllSchemes() and allowSchemes() properties.

Implementation Note:
This annotation requires the commons-validator dependency when using it, since the ValidURLValidator uses Apache Commons Validator's UrlValidator internally.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Whether to allow all URL schemes.
    boolean
    Whether to allow URLs with fragments.
    boolean
    Whether to allow local URLs (e.g., localhost, localdomain).
    boolean
    Whether to allow null values.
    The allowed URL schemes.
    boolean
    Whether to allow URLs with two consecutive slashes.
    Class<?>[]
    The validation groups to which this constraint belongs.
    The error message to use when validation fails.
    Class<? extends jakarta.validation.Payload>[]
    The payload associated with the constraint.
  • Element Details

    • message

      String message
      The error message to use when validation fails.
      Returns:
      the error message
      Default:
      "{org.kiwiproject.validation.ValidURL.message}"
    • groups

      Class<?>[] groups
      The validation groups to which this constraint belongs.

      This is a standard property from the Bean Validation API.

      Returns:
      the validation groups
      Default:
      {}
    • payload

      Class<? extends jakarta.validation.Payload>[] payload
      The payload associated with the constraint.

      This is a standard property from the Bean Validation API.

      Returns:
      the payload
      Default:
      {}
    • allowNull

      boolean allowNull
      Whether to allow null values.

      If set to true, null values will be considered valid. If set to false (the default), null values will be considered invalid.

      Returns:
      true if null values are allowed, false otherwise
      Default:
      false
    • allowAllSchemes

      boolean allowAllSchemes
      Whether to allow all URL schemes.

      If set to true, all URL schemes will be allowed. If set to false (the default), only the schemes specified in allowSchemes() will be allowed.

      Returns:
      true if all schemes are allowed, false otherwise
      Default:
      false
    • allowSchemes

      String[] allowSchemes
      The allowed URL schemes.

      This property is only used if allowAllSchemes() is set to false. By default, only "http" and "https" schemes are allowed.

      Returns:
      the allowed URL schemes
      Default:
      {"http", "https"}
    • allowLocalUrls

      boolean allowLocalUrls
      Whether to allow local URLs (e.g., localhost, localdomain).

      If set to true (the default), local URLs like http://localhost will be considered valid. If set to false, local URLs will be considered invalid.

      Returns:
      true if local URLs are allowed, false otherwise
      Default:
      true
    • allowTwoSlashes

      boolean allowTwoSlashes
      Whether to allow URLs with two consecutive slashes.

      If set to true, URLs with two consecutive slashes (other than after the scheme) will be considered valid. If set to false (the default), such URLs will be considered invalid.

      Returns:
      true if URLs with two consecutive slashes are allowed, false otherwise
      Default:
      false
    • allowFragments

      boolean allowFragments
      Whether to allow URLs with fragments.

      If set to true (the default), URLs with fragments (the part after #) will be considered valid. If set to false, URLs with fragments will be considered invalid.

      Returns:
      true if URLs with fragments are allowed, false otherwise
      Default:
      true