简体   繁体   English

除Hibernate-validator之外的Jersey Bean验证

[英]Jersey Bean validation other than hibernate-validator

When i Try to validate email id using jersey the built in hibernate-validator is not validating it properly the fallowing string lafalsdf@ga get evaluatied to valid and it fallows JSR 303 which is old .I need library which validates according to JSR 311 & JSR 339 . 当我尝试使用泽西验证电子邮件ID时,内置的hibernate-validator无法正确验证它, 休假字符串lafalsdf @ ga被评估为有效,并且使JSR 303 失效 。我需要根据JSR 311和JSR进行验证的库339。 Or any other library which is better than hibernate-validator 或任何其他比hibernate-validator更好的库

If there is any library please suggest me . 如果有图书馆,请建议我。 I have done research and was not able to find any. 我已经做过研究,却找不到任何东西。

And also give me code to configure it in jersey 并给我代码在jersey中进行配置

First of all, an emailId like lafalsdf@ga is valid as per RFC 822 ( see here ) because ga is a valid local host name. 首先,根据RFC 822,像lafalsdf @ ga这样的emailId是有效的( 请参阅此处 ),因为ga是有效的本地主机名。 Since most of the email validators follow RFC 822, they too consider it valid. 由于大多数电子邮件验证程序都遵循RFC 822,因此他们也认为它是有效的。 This is the case for Hibernate Validator,Java's native InternetAddress.validate() etc. However there is Apache Commons Validator project which provides an option to ignore local addresses when performing a validation. Hibernate Validator,Java的本机InternetAddress.validate()等就是这种情况。但是, Apache Commons Validator项目提供了一个选项,可以在执行验证时忽略本地地址。

If your concern is only with email validation, you don't have to look for a replacement for Hibernate Validator. 如果您只关心电子邮件验证,则不必寻找Hibernate Validator的替代品。 You can write a custom annotation and validator which uses Apache commons validator as shown below. 您可以编写一个使用Apache commons验证器的自定义注释和验证器,如下所示。

@Constraint(validatedBy = CommonsEmailValidator.class)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface CommonsEmail {
String message() default "{org.hibernate.validator.constraints.Email.message}";

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

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

/**
 * Defines several {@code @Email} annotations on the same element.
 */
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
public @interface List {
    CommonsEmail[] value();
}

public static class CommonsEmailValidator implements ConstraintValidator<CommonsEmail, CharSequence> {

    @Override
    public void initialize(CommonsEmail annotation) {
    }

    @Override
    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
        return EmailValidator.getInstance(false).isValid(value.toString());
    }

  }
}

Now, you can use new @CommonsEmail instead of @Email . 现在,您可以使用新的@CommonsEmail而不是@Email EmailValidator is part of commons validator library. EmailValidator是Commons EmailValidator库的一部分。 So make sure commons-validator.jar is in your classpath. 因此,请确保commons-validator.jar在您的类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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