获取包含@Size 注释的字段名称及其最大长度
Get the fields name that contain @Size annotation along with their max length
我有这个实体 -
@Entity
public class Employee{
@Id
@NotNull
@Size(max=5)
private Integer employeeId;
@NotNull
@Size(max=40)
private String employeeName;
private Long employeeSalary;
}
我想获取字段的名称及其允许的最大长度。
也就是说,对于上述情况,输出应该像
employeeId - 5
employeeName - 40
我在下面创建了这个 returns 包含@Size
的字段的名称
public boolean hasSize() {
return Arrays.stream(this.getClass().getDeclaredFields())
.anyMatch(field -> field.isAnnotationPresent(Size.class));
}
public List<String> getSizeFields(){
if(hasSize()) {
Stream<Field> filter = Arrays.stream(this.getClass().getDeclaredFields())
.filter(field -> field.isAnnotationPresent(Size.class));
return filter.map(obj -> obj.getName()).collect(Collectors.toList());
}
else
return null;
}
建议我如何获得字段的最大长度。
Map<String, Integer> map = Stream.of(e.getClass().getDeclaredFields())
.filter(f -> f.isAnnotationPresent(Size.class))
.collect(Collectors.toMap(
f -> f.getName(),
f -> f.getAnnotation(Size.class).max()));
我有这个实体 -
@Entity
public class Employee{
@Id
@NotNull
@Size(max=5)
private Integer employeeId;
@NotNull
@Size(max=40)
private String employeeName;
private Long employeeSalary;
}
我想获取字段的名称及其允许的最大长度。 也就是说,对于上述情况,输出应该像
employeeId - 5
employeeName - 40
我在下面创建了这个 returns 包含@Size
的字段的名称public boolean hasSize() {
return Arrays.stream(this.getClass().getDeclaredFields())
.anyMatch(field -> field.isAnnotationPresent(Size.class));
}
public List<String> getSizeFields(){
if(hasSize()) {
Stream<Field> filter = Arrays.stream(this.getClass().getDeclaredFields())
.filter(field -> field.isAnnotationPresent(Size.class));
return filter.map(obj -> obj.getName()).collect(Collectors.toList());
}
else
return null;
}
建议我如何获得字段的最大长度。
Map<String, Integer> map = Stream.of(e.getClass().getDeclaredFields())
.filter(f -> f.isAnnotationPresent(Size.class))
.collect(Collectors.toMap(
f -> f.getName(),
f -> f.getAnnotation(Size.class).max()));