2016-04-15 52 views
4

我想添加@NotNull約束到我的Person對象,但我仍然可以@POST一個新的人與空電子郵件。我正在使用MongoDB的Spring引導休息。春季啓動啓動器數據休息,@Notnull約束不工作

實體類:

import javax.validation.constraints.NotNull; 

public class Person { 
    @Id 
    private String id; 
    private String username; 
    private String password; 
    @NotNull // <-- Not working 
    private String email; 
    // getters & setters 
} 

庫類:

@RepositoryRestResource(collectionResourceRel = "people", path = "people") 
public interface PersonRepository extends MongoRepository<Person, String> { 
} 

應用類:

@SpringBootApplication 
public class TalentPoolApplication { 
    public static void main(String[] args) { 
     SpringApplication.run(TalentPoolApplication.class, args); 
    } 
} 

的pom.xml

... 
<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.0.BUILD-SNAPSHOT</version> 
    <relativePath/> <!-- lookup parent from repository --> 
</parent> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <java.version>1.8</java.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-mongodb</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-rest</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 
... 

當我通過郵差@POST一個新的對象,如:

{ 
    "username": "deadpool", 
    "email": null 
} 

我仍然得到這個負載創建STATUS 201

{ 
    "username": "deadpool", 
    "password": null, 
    "email": null 
    .... 
    .... 
} 
+0

哪裏控制器?請顯示其代碼。 – dambros

+0

@dambros我不知道在這種情況下需要控制器。我可以做這樣的所有HTTP操作。 – oxyt

+1

好的,所以無論你收到你的參數,在對象之前用@ @ Valid標記 – dambros

回答

5

我有同樣的問題,但只是使驗證不工作對我來說,這確實與JPA和MongoDb一起工作,以挽救任何人在這方面花費時間。這不僅得到驗證工作,但我得到一個不錯的寧靜的400錯誤,而不是默認的500

不得不加入到我的build.gradle依賴

compile('org.hibernate:hibernate-validator:4.2.0.Final') 

這個配置類

@Configuration 
public class CustomRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter { 


    @Bean 
    public Validator validator() { 
     return new LocalValidatorFactoryBean(); 
    } 

    @Override 
    public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) { 
     validatingListener.addValidator("afterCreate", validator()); 
     validatingListener.addValidator("beforeCreate", validator()); 
     validatingListener.addValidator("afterSave", validator()); 
     validatingListener.addValidator("beforeSave", validator()); 
    } 
} 
+0

當我添加你的配置時,我總是收到這個錯誤:** java.lang.AbstractMethodError:org.hibernate.validator.engine.ValidatorFactoryImpl.close()V **你知道如何解決這個問題嗎? (那V最後不是打字錯誤) –

3

我發現最好使我自己版本的@NotNull註釋驗證空字符串。

@Documented 
@Constraint(validatedBy = NotEmptyValidator.class) 
@Target({ElementType.METHOD, ElementType.FIELD}) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface NotEmpty { 


    String message() default "{validator.notEmpty}"; 

    Class<?>[] groups() default {}; 

    Class<? extends Payload>[] payload() default {}; 

} 

public class NotEmptyValidator implements ConstraintValidator<NotEmpty, Object> { 

    @Override 
    public void initialize(NotEmpty notEmpty) { } 

    @Override 
    public boolean isValid(Object obj, ConstraintValidatorContext cxt) { 
     return obj != null && !obj.toString().trim().equals(""); 
    } 

} 
+0

謝謝謝赫阿卜杜勒,我會稍後再試這個:) – oxyt

0

您可以使用下面的代碼用於驗證

@Configuration 
@Import(value = MongoAutoConfiguration.class) 
public class DatabaseConfiguration extends AbstractMongoConfiguration 
{ 

    @Resource 
    private Mongo mongo; 

    @Resource 
    private MongoProperties mongoProperties; 

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

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

    @Override 
    protected String getDatabaseName() { 
    return mongoProperties.getDatabase(); 
    } 

    @Override 
    public Mongo mongo() throws Exception { 
    return mongo; 
    } 

}