JavaSpring启动后请求验证
我有一个名为Credentials的类,它有两个字符串成员变量:username和password
我还有一门课叫Profile。Profile有一组成员变量,包括两个Credentials对象
public class Profile {
@NotNull
@NotEmpty
private String id;
@NotNull
@NotEmpty
private String businessAlias;
@NotNull
@NotEmpty
private String localParty;
...
private PartyCredentials localPartyCredentials ;
private PartyCredentials remotePartyCredentials;
}
在POST映射期间,POST正文Json可能包含也可能不包含localPartyCredentials和remotePartyCredentials。如果它们包含其中任何一个,我想确保“localPartyCredentials”或“remotePartyCredentials”同时包含“用户名”和“密码”密钥,否则这是一个无效的请求
这是有效的
"localPartyCredentials":{
"username":"one",
"password": two
}
如果“localPartyCredentials”JsonNode不存在,它也是一个有效的请求。但如果“用户名”或“密码”或两者都缺失,则无效
在PartyCredentials中将@NotNull和@NotEmpty添加到用户名和密码中似乎不起作用。以下是post req的映射:
@PostMapping(path = "/", consumes = "application/json", produces = "application/json")
public ResponseEntity<?> create(
@NotNull @Valid @RequestBody Profile profile) throws JsonProcessingException {
下面是我的设置程序在凭据对象配置文件中的样子:
public void setLocalPartyCredentials(PartyCredentials localPartyCredentials) {
this.localPartyCredentials = localPartyCredentials;
}
共 (0) 个答案