gpt4 book ai didi

java - Spring验证@NotNull不验证

转载 作者:行者123 更新时间:2023-11-29 08:27:15 25 4
gpt4 key购买 nike

我正在尝试使用 Spring Validator 验证 POST 请求。这是我要验证的对象:

@Validated
public class Request implements Serializable {
private static final long serialVersionUID = 1L;

private Long id1;
private Long id2;
private String s;
private List<Mapping> mappings;

public Request() {}
public Request (Long id1, Long id2, String s, List<Mapping> mappings) {
this.id1 = id1;
this.id2 = id2;
this.s = s;
this.mappings = mappings;
}

//getter() and setter() methods.
}

这是映射类:

@Validated
public class Mapping implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty(value="id")
private Long id;

@JsonProperty(value="string")
private String string;

@JsonProperty(value="account_id")
@NotNull
private String accountId;

@JsonProperty(value="last_updated_at")
@NotNull
private OffsetDateTime lastUpdatedAt;

// Getter and setters()
// I'll skip the getter() and setter() for id and string fields.
@Override
public String getAccountId() {
return accountId;
}

@Override
public void setAccountId (@NotNull String accountId) {
this.accountId = accountId;
}

@Override
public OffsetDateTime getLastUpdatedAt() {
return lastUpdatedAt;
}

@Override
public void setLastUpdatedAt(@NotNull OffsetDateTime lastUpdatedAt) {
this.lastUpdatedAt = lastUpdatedAt;
}
}

这是正在发布的消息:

[Request [id1=null, id2=null, s=6eq2J6, mappings=

[Mapping [id=2779, string=6eq2J6, accountId=null, lastUpdatedAt=null]]]]

这是我收到请求的方式,但 bindingResult.hasErrors() 始终为空。 pushRequest 与发送时完全相同。

@RestController
@RequestMapping(value="/${path}", method={RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT})
@Validated
public class MappingController {

@PostMapping(value="/", consumes={"application/json"})
public void processMappings (@Valid @RequestBody Optional<Request> pushRequest, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
log.info("Error!");
}
}
}

为什么当 accountId 和 lastUpdatedAt 具有 Null 值时,@NotNull 不会使验证失败?

我试图通过搜索此错误找到解决方案,解决方案是将 @Validated 添加到 Request、Mapping 和 MappingController 类中,但没有解决问题。

另一种解决方案是将这些Bean添加到Main()类中,但没有解决问题。

public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
//System.out.println("version: " + SpringVersion.getVersion()); // version: 5.1.0.RC1
}

@Bean
public javax.validation.Validator localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
MethodValidationPostProcessor mvProcessor = new MethodValidationPostProcessor();
mvProcessor.setValidator(validator());
return mvProcessor;
}

@Bean
public LocalValidatorFactoryBean validator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setProviderClass(HibernateValidator.class);
validator.afterPropertiesSet();
return validator;
}
}

最佳答案

我想我找到了这个帖子的答案:

jsr-303-valid-annotation-nested-object-not-working

所以当我将@Valid 添加到这些字段时:

@Valid
private List<Mapping> mappings;

Mapping 中的这些字段:

@JsonProperty(value="account_id")
@Valid
@NotNull
private String accountId;

@JsonProperty(value="last_updated_at")
@Valid
@NotNull
private OffsetDateTime lastUpdatedAt;

验证开始工作。

我还删除了 setter() 方法中的 @NotNull:

Setters() 像这样:

@Override
public void setAccountId (String accountId) {
this.accountId = accountId;
}

@Override
public void setLastUpdatedAt(OffsetDateTime lastUpdatedAt) {
this.lastUpdatedAt = lastUpdatedAt;
}

我的代码也可以在没有添加到 Main() 的任何 validator bean 的情况下工作。

关于java - Spring验证@NotNull不验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51604833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com