基于@GroupSequence 实现入参顺序校验

校验接口
基于@GroupSequence 注解实现 参数的顺序校验

@GroupSequence({VerifySeq.N0.class, VerifySeq.N1.class, VerifySeq.N2.class,VerifySeq.N3.class,

    VerifySeq.N4.class,VerifySeq.N5.class,VerifySeq.N6.class, VerifySeq.N7.class,
    VerifySeq.N8.class, VerifySeq.N9.class, Default.class})

public interface VerifySeq {

interface N0 {
}
interface N1 {
}
interface N2 {
}
interface N3 {
}
interface N4 {
}
interface N5 {
}
interface N6 {
}
interface N7{
}
interface N8 {
}
interface N9 {
}

}

校验类
重点在校验注解上添加 groups = VerifySeq.N1.class。可以根据具体的业务场景来实现参数的校验顺序。

@Data
public class SecurityQuestionVfyDTO {

@NotBlank(message = "###err.param.ada.isNotEmpty###", groups = VerifySeq.N1.class)
@Pattern(regexp = ADA_FORMAT_ALLOW_BLANK, message = "###error.param.format.ada###", groups = VerifySeq.N2.class)
@ApiModelProperty(value = "号码", required = true, example = "63304")
private String ada;

@NotBlank(message = "###err.param.name.isNotEmpty###", groups = VerifySeq.N5.class)
@ApiModelProperty(value = "姓名", required = true, example = "张三")
private String name;

@NotBlank(message = "###err.param.qid.isNotEmpty###", groups = VerifySeq.N3.class)
@ApiModelProperty(value = "问题ID", required = true, example = "1")
private String qid;

@Length(max=60, message = "###err.param.answer.tooLong###")
@NotBlank(message = "###err.param.answer.isNotEmpty###", groups = VerifySeq.N4.class)
@ApiModelProperty(value = "答案", required = true, example = "第一中学")
private String answer;

}
使用
在需要校验的接口处添加@Validated(VerifySeq.class)

public interface ISecurityQuestionClient {

@PostMapping(path = "/***/***/***", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
WebResult verifySecurityQuestion(@Validated(VerifySeq.class) @RequestBody SecurityQuestionVfyDTO securityQuestionVfyDTO);

}

上一篇:eclipes创建一个web项目web.xml不能自动更新的原因(web.xml和@WebServlet的作用)


下一篇:Java连接Oracle数据库的三种连接方式