我用java 1.8,春季引導和MongoDB的一些數據存儲在數據庫中,以瞭解MongoDB的註釋是否可以限制java中類字段的字符?
我要限制通過註釋的類領域的人物,我試圖用Hibernate驗證器來控制這一點:
import javax.validation.constraints.Size;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Document(collection = "MusicGroup")
public class MusicGroup {
@Id
private String id;
@Size(min = 7, max = 7, message = "title need to have only 4 characters")
@Field("Title")
private String title;
@Size(min = 11, max = 11, message = "type need to have only 11 characters")
@Field("Type")
private String type;
@Field("Members")
private List<String> members;
public MusicGroup(String titulo, String tipo, List<String> integrantes) {
this.title = titulo;
this.type = tipo;
this.members = integrantes;
}
但是當我插入的MongoDB絲毫此驗證碼:
@Service
public class MusicGroupServiceImpl implements MusicGroupService {
@Autowired
private MusicGroupRepository repository;
public MusicGroupServiceImpl() {
}
@Override
public void storeGroup(){
String[] memberList = new String[3];
memberList[0] = "A";
memberList[1] = "B";
memberList[2] = "C";
MusicGroup group = new MusicGroup("Panteraaaaaa", "Heavy Metallllllllllllllll", memberList);
repository.save(group);
}
}
它的作品沒有任何問題的時候Panteraaaaaa的長度比7和重型Metallllllllllllllll相同更大
附上對應到我的pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
爲了實現這個問題 可以通過註釋來控制Java字段的字符長度德下面的代碼?或者我需要做一個方法來控制這個
你可以試試,如果從http://stackoverflow.com/questions/22568962/how-to接受的答案-i-get-spring-data-mongodb-to-validate-my-objects也適用於你? – dunni
感謝您的評論,已解決的問題並不完全是我的問題的解決方案,但它引導了我,我將在幾分鐘內放入我所做的驗證字段大小的工作 – MaQuiNa1995