我想添加@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
....
....
}
哪裏控制器?請顯示其代碼。 – dambros
@dambros我不知道在這種情況下需要控制器。我可以做這樣的所有HTTP操作。 – oxyt
好的,所以無論你收到你的參數,在對象之前用@ @ Valid標記 – dambros