简体   繁体   English

Spring Boot MongoRepository忽略验证

[英]Spring Boot MongoRepository ignoring validation

I have a Mongodb repository that is working fine: 我有一个工作正常的Mongodb存储库:

@RepositoryRestResource(collectionResourceRel = "audits", path = "audits")
public interface AuditRepository extends MongoRepository<Audit, String> {
}

I have a bean, Audit that is: 我有一个bean, Audit是:

@Data
@Document
@JsonIgnoreProperties(ignoreUnknown = true)
@Validated
public class Audit {
    @Id private String id;

    @NotNull
    private Date start;

    @NotNull
    private Date end;
}

I'm using Lombok for getters/setters. 我正在使用Lombok获取getter / setter。

I expect the Repository to validate the Audit bean, but it saves an audit bean with null in the start and end date. 我希望Repository验证Audit bean,但它会在开始日期和结束日期保存一个null的审计bean。

I added this to the build.gradle : 我将它添加到build.gradle

compile("org.springframework.boot:spring-boot-starter-validation")

How do I tell the REST service to use validation? 如何告诉REST服务使用验证? I don't see anything in RepositoryRestConfiguration that will turn it on... 我没有在RepositoryRestConfiguration中看到任何会打开它的东西......

You must import validations libs: 您必须导入验证库:

maven 行家

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.4.2.Final</version>
</dependency>

or gradle 或者是gradle

compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.4.2.Final'

and you must configure two beans: 你必须配置两个bean:

@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
    return new LocalValidatorFactoryBean();
}

@Bean
public ValidatingMongoEventListener validatingMongoEventListener(LocalValidatorFactoryBean lfb) {
    return new ValidatingMongoEventListener(lfb);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM