Annotation Type URL
-
@Documented @Constraint(validatedBy={}) @Target({METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE}) @Retention(RUNTIME) @Repeatable(List.class) @ReportAsSingleViolation @Pattern(regexp="") public @interface URL
Validates the annotated string is an URL.The parameters
protocol,hostandportare matched against the corresponding parts of the URL. and an additional regular expression can be specified usingregexpandflagsto further restrict the matching criteria.Note: Per default the constraint validator for this constraint uses the
java.net.URLconstructor 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:
or via a constraint mapping configuration: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 );<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,
ConstraintMapping.constraintDefinition(Class)
-
-
Element Detail
-
message
String message
- Default:
- "{org.hibernate.validator.constraints.URL.message}"
-
-
-
groups
Class<?>[] groups
- Default:
- {}
-
-
-
protocol
String protocol
- Returns:
- the protocol (scheme) the annotated string must match, e.g. ftp or http. Per default any protocol is allowed
- Default:
- ""
-
-
-
host
String host
- Returns:
- the host the annotated string must match, e.g. localhost. Per default any host is allowed
- Default:
- ""
-
-
-
regexp
@OverridesAttribute(constraint=Pattern.class, name="regexp") String regexp
- Returns:
- an additional regular expression the annotated URL must match. The default is any string ('.*')
- Default:
- ".*"
-
-
-
flags
@OverridesAttribute(constraint=Pattern.class, name="flags") Pattern.Flag[] flags
- Returns:
- used in combination with
regexp()in order to specify a regular expression option
- Default:
- {}
-
-