简体   繁体   English

Spring 使用 Enum 请求 notempty 验证

[英]Spring request notempty validation with Enum

I've a request with an Enum inside and I can validate it with @NotNull but if I try to insert @NotEmpty too it does not work.我有一个内部包含 Enum 的请求,我可以使用 @NotNull 对其进行验证,但如果我尝试插入 @NotEmpty 也不起作用。 This is a sample of what I would like to achieve.这是我想要实现的示例。

@NotNull
@NotEmpty
private MyEnum myEnum;

How can I make it works?我怎样才能让它工作?

javax @NotEmpty can only be applied CharSequence, Collection, Map or Array values (see documentation) javax @NotEmpty 只能应用于 CharSequence、Collection、Map 或数组值(参见文档)

Empty and enum does not realy make sense, except if you have an enum with an empty value, for example:空和枚举实际上没有意义,除非你有一个空值的枚举,例如:

public enum MyEnum {
    NOT_EMPTY("notEmpty"), EMPTY("");
}

So the question would be how to validate for specific enum values or subsets.所以问题是如何验证特定的枚举值或子集。

In that case you would have to define your own validation annotation.在这种情况下,您必须定义自己的验证注释。

Like Shailesh Chandra has mentioned in the comment we don't need to mention @NotEmpty or @NotBlank annotations for ENUM types.就像 Shailesh Chandra 在评论中提到的那样,我们不需要为 ENUM 类型提及 @NotEmpty 或 @NotBlank 注释。

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

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