如何在不违反其他输入字段的验证的情况下传递所需参数

时间:2016-02-02 05:43:43

标签: java validation rest hibernate-validator spring-rest

我为模块的所有参数创建了一个输入传递参数类,&想要为不同的操作使用不同的参数

我的传递参数类的字段为

private Integer userId;
@Id
@Column(name = "user_email_id")
@NotEmpty(message = "Please enter your email_Id.")
@Email
@NotNull(message = "Enter last name")
private String userEmailId;
@NotEmpty(message = "Please enter your password.")
@Column(name = "user_password")
private String userPassword;
@NotNull(message = "Enter last name")
@NotEmpty(message = "Please enter your firstName.")
@Column(name = "firstname")
@SafeHtml()
private String firstName;
@SafeHtml()
@NotNull(message = "Enter last name")
@NotEmpty(message = "Please enter your lastName.")
@Column(name = "lastname")
private String lastName;
@NotEmpty
@Pattern(regexp = "(^[0-9]{10,12}$)", message = "Please enter your Mobile Number.")
@NumberFormat
@Column(name = "mobile_number")
private String mobileNumber;
@Column(name = "user_status")
private Integer userStatus;
@Column(name = "isdeleted")
private Integer isDeleted;
@Column(name = "createdUserId")
private Integer createdUserId;
@Column(name = "profile_picturename")
private String profilePicturename;
@Column(name = "address")
private String address;

private Integer isAdmin;

此处注册我使用

 {  
"userEmailId": "sdfdfgfgf",
"userPassword": "fdsdsdf",
"firstName": "sdfsdf",
"lastName": "sfdsdfds",
"mobileNumber": "sdfgdgdf",
"userStatus": 1,
"isDeleted": 0,
"createdUserId": 1,
"profilePictureName": "kfksdjfhksjd",
"address": "sfdsdfsd"

}

传递json&中的参数登录尝试传递

{
"userEmailId": "f@g.cm",
"userPassword": "fdsdsdf",
"isAdmin":0

}

参数,但对于登录我也获得其他字段的验证消息,

任何人都可以告诉我如何才能将所需的字段传递给方法,而不会获得我未通过的字段的验证消息

提前举手

2 个答案:

答案 0 :(得分:1)

如果您的要求是仅传递必填字段,那么我觉得您不应该将@notnull用于其他非必填字段,如下所示:

private Integer userId;
@Id
@Column(name = "user_email_id")
@NotEmpty(message = "Please enter your email_Id.")
@Email
@NotNull(message = "Enter last name")
private String userEmailId;
@NotEmpty(message = "Please enter your password.")
@Column(name = "user_password")
private String userPassword;
@NotEmpty(message = "Please enter your firstName.")
@Column(name = "firstname")
@SafeHtml()
private String firstName;
@SafeHtml()
@NotEmpty(message = "Please enter your lastName.")
@Column(name = "lastname")
private String lastName;
@NotEmpty
@Pattern(regexp = "(^[0-9]{10,12}$)", message = "Please enter your Mobile Number.")
@NumberFormat
@Column(name = "mobile_number")
private String mobileNumber;
@Column(name = "user_status")
private Integer userStatus;
@Column(name = "isdeleted")
private Integer isDeleted;
@Column(name = "createdUserId")
private Integer createdUserId;
@Column(name = "profile_picturename")
private String profilePicturename;
@Column(name = "address")
private String address;

private Integer isAdmin;

答案 1 :(得分:1)

我不太确定将所有潜在参数分组到单个类中是否有意义,但如果您只是想要根据特定用例验证给定子集,则需要使用验证组。查看Hibernate Validator在线文档以了解此功能。您基本上将不同的约束分配给不同的组,例如Login。当您再调用Validator#validate时,您也会传递要验证的组。如果您自己控制验证不是问题。如果您正在使用其他框架,而这些框架又与Bean Validation集成,则需要检查如何验证给定组。