I am trying to add an @NotNull constraint to my Person object, but I can still @POST a new person with a null email address. I am using Spring boot with MongoDB.
Entity Class:
import javax.validation.constraints.NotNull;
public class Person {
@Id
private String id;
private String username;
private String password;
@NotNull
private String email;
}
Repository Class:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends MongoRepository<Person, String> {
}
Application Class:
@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/>
</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>
...
When I @POST a new object through Postman, for example:
{
"username": "deadpool",
"email": null
}
I still get STATUS 201created using this payload:
{
"username": "deadpool",
"password": null,
"email": null
....
....
}
source
share