简体   繁体   中英

URL validator for JEE6 Beans Validation 1.0

How can I create a custom validator for a URL field in Beans Validation 1.0. But how can I pass the URL string passed via JSON into my constraint validator?

@ValidURL
private URL myUrl;

public class URLValidator implements ConstraintValidator<ValidURL, CharSequence> {...}

@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Constraint(validatedBy = {URLValidator.class})
@Constraint(validatedBy = {})
public @interface ValidURL {

    String message() default "must be a valid URL";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

I know the latest Hibernate Validator has a @URL validation but I cannot move to the latest version.

Edit: With the above I am hitting the following. So my question is how does Hibernate convert the URL type to the CharSequence used by the ConstraintValidator?

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.net.URL.

Doh,刚意识到Hibernate验证程序@URL是针对String类型的,而不是java.net.URL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM