验证 UUID Restful 服务 [英] Validate UUID Restful service

查看:24
本文介绍了验证 UUID Restful 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RESTful 服务,它接收带有 UUID 值的 POST 请求并将它们写入数据库.所以问题是验证 UUID 是否有效.为此,我实现了自定义注释:

I have a RESTful service which receives POST request with UUID values and writes them in DB. So the problem is to validate if UUID is valid or not. For this purpose I implemented custom annotation:

@Constraint(validatedBy = {})
@Target({ElementType.FIELD})
@Retention(RUNTIME)
@Pattern(regexp = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[34][0-9a-fA-F]{3}-[89ab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}")
public @interface validUuid {

    String message() default "{invalid.uuid}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

但由于某种原因它不起作用,即使我通过了有效的UUID,我也经常收到一个异常:

But for some reason it doesn't work, even if I pass valid UUID I constantly get an exception:

javax.validation.UnexpectedTypeException: HV000030: 没有验证器可以找到约束'javax.validation.constraints.Pattern'验证类型 'java.util.UUID'

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Pattern' validating type 'java.util.UUID'

是否有任何选项可以正确验证 UUID?

Are there any options to validate UUID properly?

推荐答案

您不能应用 @Pattern 注释(java.util.UUID) 不是 CharSequence.来自 @Pattern 注释文档(强调我的):

You cannot apply the @Pattern annotation to something (java.util.UUID) that is not a CharSequence. From the @Pattern annotation documentation (emphesizes mine):

接受CharSequence.null 元素被认为是有效的.

Accepts CharSequence. null elements are considered valid.

此外,据我所知,您尝试通过将验证注释处理程序传递给新的注释定义来扩展它的行为.

Moreover, as far as I see you try to extend the behavior of the validation annotation handler by passing it to the new annotation definition.

如果您希望执行更复杂的验证,只需创建您的注释而无需另一个验证注释 - 它们的组合不会像这样工作.必须有东西来识别注释并验证它们.

If you wish to perform more complex validation, simply create your annotation without another validation annotations - their combining doesn't work like this. There must be something to recognize annotations and validate them.

@Target({ElementType.FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = UuidValidator.class)
public @interface ValidUuid {

    String message() default "{invalid.uuid}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

现在,创建一个实现 ConstraintValidator< 的验证器;ValidUuid, UUID> 并覆盖执行验证本身的方法.

Now, create a validator which implements ConstraintValidator<ValidUuid, UUID> and override the methods performing the validation itself.

public class UuidValidator implements ConstraintValidator<ValidUuid, UUID> {
    private final String regex = "....." // the regex

    @Override
    public void initialize(ValidUuid validUuid) { }

    @Override
    public boolean isValid(UUID uuid, ConstraintValidatorContext cxt) {
        return uuid.toString().matches(this.regex);
    }
}

并应用注释:

@ValidUuid
private UUID uuId;

这篇关于验证 UUID Restful 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆