简体   繁体   中英

Validating POJO with JSR 303

I have a POJO as follows:

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".

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 ?

for isActive you could use @Pattern(regexp="(true|false)") for programList you could use @Size(2) with @Pattern(regexp="(Test1|Test2) ?

I didn't tested that, but you can do it on your own.

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