@Documented @Constraint(validatedBy={}) @Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER}) @Retention(value=RUNTIME) @Pattern(regexp="") public @interface 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 ConstraintDefinitionContributor
:
HibernateValidatorConfiguration configuration = Validation
.byProvider( HibernateValidator.class )
.configure();
configuration.addConstraintDefinitionContributor(
new ConstraintDefinitionContributor() {
public void collectConstraintDefinitions(ConstraintDefinitionBuilder builder) {
builder.constraint( URL.class )
.includeExistingValidators( false )
.validatedBy( RegexpURLValidator.class );
}
}
);
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>
ConstraintDefinitionContributor
public abstract String message
public abstract Class<?>[] groups
public abstract String protocol
public abstract String host
public abstract int port
public abstract String regexp
public abstract Pattern.Flag[] flags
regexp()
in order to specify a regular expression optionCopyright © 2016 JBoss by Red Hat. All rights reserved.