{"title":"Java Bean 字段校验:UserValidated 示例","description":"本文提供了一个 Java Bean 字段校验的示例代码,使用 @NotNull、@NotBlank、@Pattern 等注解对 UserValidated 类中的字段进行校验,并给出详细的校验规则和错误提示信息。","keywords":"Java Bean, 字段校验, @NotNull, @NotBlank, @Pattern, UserValidated, 示例代码","content":"public class UserValidated implements Serializable {\n\n @ApiModelProperty(value = "账号")\n @NotNull(message = "账号不能为空")\n private String userAccount;\n\n @ApiModelProperty(value = "密码1")\n @NotBlank(message = "密码1不能为空")\n private String password1;\n\n @ApiModelProperty(value = "密码2")\n @NotBlank(message = "密码2不能为空")\n private String password2;\n\n @ApiModelProperty(value = "手机号")\n @Pattern(regexp = "^1[0-9]{10}$")\n private String userPhone;\n\n @ApiModelProperty(value = "用户是否绑定服务号(notbound:未绑定binding:绑定)")\n private String subscribe;\n\n @ApiModelProperty(value = "微信用户唯一id标识")\n private String userOpenid;\n\n // getters and setters\n}\n\n在上述代码中,使用了以下注解来完成字段的校验:\n\n1. @NotNull:用于标记 userAccount 字段不能为空。\n2. @NotBlank:用于标记 password1password2 字段不能为空字符串。\n3. @Pattern:用于标记 userPhone 字段必须符合指定的正则表达式,此处使用了手机号的正则表达式。\n4. @ApiModelProperty:用于给字段添加说明信息,对校验没有直接影响,但可以提高代码的可读性。\n\n请根据实际需求修改校验规则和错误提示信息。"}

Java Bean 字段校验:UserValidated 示例

原文地址: https://www.cveoy.top/t/topic/qgXT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录