注释可以限制 java 中 class 字段的字符吗?
Can annotations limit the characters of a class field in java?
我正在使用 java 1.8,Spring Boot 和 MongoDb 在数据库中存储一些数据以了解 mongoDb
我想通过注释限制 class 字段的字符 我试图用 hibernate-validator 控制它:
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 且 Heavy 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 中字段字符的长度?或者我需要做一个方法来控制这个
您没有验证对象。只是给予限制。你能使你的方法像下面这样获取对象并重试吗?
storeGroup (@Validated MusicGroup group) {
....
}
编辑:检查@Validated 和@Valid 注释。
大家好我终于找到了字段验证大小的解决方案
我只需要在这里做一个新的class代码:
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@SpringBootApplication
public class Configuration {
@Bean
public ValidatingMongoEventListener validatingMongoEventListener() {
return new ValidatingMongoEventListener(validator());
}
@Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}
}
然后将尺寸注释的位置更改为setter
@Document(collection = "MusicGroup")
public class MusicGroup {
@Id
private String id;
@Field("Title")
private String title;
@Field("Type")
private String type;
@Field("Members")
private String[] members;
public MusicGroup() {
}
@Size(min = 7, max = 7)
public String getTitle() {
return title;
}
@Size(min = 11, max = 11)
public String getType() {
return type;
}
// Other getters, setters and to String()
}
添加到 pom:
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
当我创建一个新对象 MusicGroup 时:
String[] memberList = new String[3];
memberList[0] = "typeA";
memberList[1] = "typeB";
memberList[2] = "typec";
MusicGroup grupo = new MusicGroup();
grupo.setType("888888888889");
grupo.setTitle("hhhhhhh");
grupo.setIntegrantes(memberList);
musicaService.storeGroup(grupo);
如果你在 运行 程序时这样做,它会给出这样的异常:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at es.cic.cmunoz.MongoDbMascotaApplication.main(MongoDbMascotaApplication.java:32) [classes/:na]
Caused by: javax.validation.ConstraintViolationException: null
at org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener.onBeforeSave(ValidatingMongoEventListener.java:67) ~[spring-data-mongodb-1.10.3.RELEASE.jar:na]
...
但是如果您在此方法中输入有效的字符串
grupo.setType("88888888888");
它不会给你任何异常
感谢 dunni 的评论,它引导我找到解决方案
PD:如果你想验证像 int 这样的数字字段,你需要在 getter 方法中使用 @Max(Number) 或 @Min(Number)
我正在使用 java 1.8,Spring Boot 和 MongoDb 在数据库中存储一些数据以了解 mongoDb
我想通过注释限制 class 字段的字符 我试图用 hibernate-validator 控制它:
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 且 Heavy 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 中字段字符的长度?或者我需要做一个方法来控制这个
您没有验证对象。只是给予限制。你能使你的方法像下面这样获取对象并重试吗?
storeGroup (@Validated MusicGroup group) {
....
}
编辑:检查@Validated 和@Valid 注释。
大家好我终于找到了字段验证大小的解决方案
我只需要在这里做一个新的class代码:
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@SpringBootApplication
public class Configuration {
@Bean
public ValidatingMongoEventListener validatingMongoEventListener() {
return new ValidatingMongoEventListener(validator());
}
@Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}
}
然后将尺寸注释的位置更改为setter
@Document(collection = "MusicGroup")
public class MusicGroup {
@Id
private String id;
@Field("Title")
private String title;
@Field("Type")
private String type;
@Field("Members")
private String[] members;
public MusicGroup() {
}
@Size(min = 7, max = 7)
public String getTitle() {
return title;
}
@Size(min = 11, max = 11)
public String getType() {
return type;
}
// Other getters, setters and to String()
}
添加到 pom:
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
当我创建一个新对象 MusicGroup 时:
String[] memberList = new String[3];
memberList[0] = "typeA";
memberList[1] = "typeB";
memberList[2] = "typec";
MusicGroup grupo = new MusicGroup();
grupo.setType("888888888889");
grupo.setTitle("hhhhhhh");
grupo.setIntegrantes(memberList);
musicaService.storeGroup(grupo);
如果你在 运行 程序时这样做,它会给出这样的异常:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at es.cic.cmunoz.MongoDbMascotaApplication.main(MongoDbMascotaApplication.java:32) [classes/:na]
Caused by: javax.validation.ConstraintViolationException: null
at org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener.onBeforeSave(ValidatingMongoEventListener.java:67) ~[spring-data-mongodb-1.10.3.RELEASE.jar:na]
...
但是如果您在此方法中输入有效的字符串
grupo.setType("88888888888");
它不会给你任何异常
感谢 dunni 的评论,它引导我找到解决方案
PD:如果你想验证像 int 这样的数字字段,你需要在 getter 方法中使用 @Max(Number) 或 @Min(Number)