简体   繁体   English

使用JSR 303验证POJO

[英]Validating POJO with JSR 303

I have a POJO as follows: 我有一个POJO如下:

public class ClosureCodeReasonRequest {
    @NotNull(message = MessageConstants.CLOSURE_CODE_BLANK_ERROR)
    @NotBlank(message = MessageConstants.CLOSURE_CODE_BLANK_ERROR)
    private String closureCode;

    @NotNull(message = MessageConstants.REASON_TITLE_BLANK_ERROR)
    @NotBlank(message = MessageConstants.REASON_TITLE_BLANK_ERROR)
    @Size(max = 50, message = MessageConstants.REASON_TITLE_TOO_LONG)
    private String reasonTitle;

    @NotEmpty
    private List<String> programList;

    @NotNull
    @NotBlank
    private String isActive;

    @NotNull
    @NotBlank
    private Long version;
}

In the above POJO, value of isActive can be either "true" or "false" and length of programList can be either 1 or 2 and contents will be among "Test1" and "Test2". 在上面的POJO中, isActive值可以为“ true”或“ false”, programList长度可以为1或2,并且内容将在“ Test1”和“ Test2”之间。

Is there any built in annotation that can be used for these requirements or do I have to create a new one? 是否有任何可用于这些要求的内置注释,还是我必须创建一个新的注释?

What about using @Pattern ? 使用@Pattern怎么

for isActive you could use @Pattern(regexp="(true|false)") for programList you could use @Size(2) with @Pattern(regexp="(Test1|Test2) ? 对于isActive,您可以将@Pattern(regexp="(true|false)")用于programList,可以将@Size(2)@Pattern(regexp="(Test1|Test2)

I didn't tested that, but you can do it on your own. 我没有测试过,但是您可以自己完成。

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

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