Top Description Inners Methods
org.hibernate.validator.constraints

public @Interface URL

extends Annotation
Annotations
@Documented
@Constraint
validatedBy:
@Target:METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE
@Retention:RUNTIME
@Repeatable:List
@ReportAsSingleViolation
@Pattern
regexp:
Static Imports
java.lang.annotation.ElementType.ANNOTATION_TYPE, .ElementType.CONSTRUCTOR, .ElementType.FIELD, .ElementType.METHOD, .ElementType.PARAMETER, .ElementType.TYPE_USE, .RetentionPolicy.RUNTIME

Validates the annotated string is an URL.

The parameters protocol, host and port are matched against the corresponding parts of the URL. and an additional regular expression can be specified using regexp and flags to further restrict the matching criteria.

Note

Per default the constraint validator for this constraint uses the java.net.URL constructor to validate the string. This means that a matching protocol handler needs to be available. Handlers for the following protocols are guaranteed to exist within a default JVM - http, https, ftp, file, and jar. See also the Javadoc for URL.

In case URLs with non default protocol handlers need to be validated, Hibernate Validator can be configured to use a regular expression based URL validator only. This can be done programmatically via a org.hibernate.validator.cfg.ConstraintMapping:

HibernateValidatorConfiguration config = Validation.byProvider( HibernateValidator.class )
    .getConfiguration( HibernateValidator.class );

ConstraintMapping constraintMapping = config.createConstraintMapping();
constraintMapping
    .constraintDefinition( URL.class )
    .includeExistingValidators( false )
    .validatedBy( RegexpURLValidator.class );

config.addMapping( constraintMapping );

or via a constraint mapping configuration:
<constraint-mappings
    xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd">

    <constraint-definition annotation="org.hibernate.validator.constraints.URL">
        <validated-by include-existing-validators="false">
            <value>org.hibernate.validator.constraintvalidators.RegexpURLValidator</value>
        </validated-by>
    </constraint-definition>
</constraint-mappings>

Author
Hardy Ferentschik
See Also
RFC2396, org.hibernate.validator.cfg.ConstraintMapping#constraintDefinition(Class)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static @interface
URL.List

Defines several @URL annotations on the same element.

Method Summary

Modifier and TypeMethod and Description
public Pattern.Flag[]

Returns:

used in combination with regexp() in order to specify a regular expression option
flags
()

public Class<?>[]
public String

Returns:

the host the annotated string must match, e.g. localhost. Per default any host is allowed
host
()

public String
public Class<? extends Payload>[]
public int

Returns:

the port the annotated string must match, e.g. 80. Per default any port is allowed
port
()

public String

Returns:

the protocol (scheme) the annotated string must match, e.g. ftp or http. Per default any protocol is allowed
protocol
()

public String

Returns:

an additional regular expression the annotated URL must match. The default is any string ('.*')
regexp
()

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

flagsback to summary
public Pattern.Flag[] flags()
Returns:Pattern.Flag[]

used in combination with regexp() in order to specify a regular expression option

Annotations
@OverridesAttribute
constraint:Pattern
name:flags
groupsback to summary
public Class<?>[] groups()
hostback to summary
public String host()
Returns:String

the host the annotated string must match, e.g. localhost. Per default any host is allowed

messageback to summary
public String message()
payloadback to summary
public Class<? extends Payload>[] payload()
portback to summary
public int port()
Returns:int

the port the annotated string must match, e.g. 80. Per default any port is allowed

protocolback to summary
public String protocol()
Returns:String

the protocol (scheme) the annotated string must match, e.g. ftp or http. Per default any protocol is allowed

regexpback to summary
public String regexp()
Returns:String

an additional regular expression the annotated URL must match. The default is any string ('.*')

Annotations
@OverridesAttribute
constraint:Pattern
name:regexp
org.hibernate.validator.constraints back to summary

public @Interface URL.List

extends Annotation
Annotations
@Target:METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE
@Retention:RUNTIME
@Documented

Defines several @URL annotations on the same element.

Method Summary

Modifier and TypeMethod and Description
public URL[]
Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

valueback to summary
public URL[] value()