java Bean验证列表<?>关于静态方法或getter
在jersey上触发自定义约束验证时出现问题。我想激活方法或静态方法上的约束。我尝试过的是在方法的顶部放置一个自定义注释和@ValidateOnExecution
,但是自定义验证器类仍然没有被触发
@LocationIsValid
@ValidateOnExecution
public static List<Double> getLocation(String location) {
...
}
我怀疑问题在于bean注释不支持静态方法,所以我删除了static关键字,并通过创建一个新对象来访问该方法。但是,自定义LocationIsValid
验证器仍然没有激活
因此,我最终放置了一个验证器工厂来手动验证这个变量
public static List<Double> getLocation(String location) {
...
// split the location string into a list of double
...
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
validator.validate(location, LocationIsValid.class);
}
但定制约束不会改变。我希望有人能给我下一步该做什么的线索,或者其他解决这个问题的建议
更多信息
当注释放置在资源字段的顶部时,它可以正常工作
public class Product {
...
@LocationIsValid
private List<Double> location;
...
}
更新
即使我已经将该方法更改为普通实例方法,它仍然不起作用。请注意,我有两个重载方法,一个是资源getter,另一个是用于将字符串转换为位置的方法
产品模型
public class Product {
@Id
@JsonSerialize(using = ObjectIdSerializer.class)
private ObjectId id;
@Size(min = 5)
private String name;
@NotNull
@LocationIsValid
private List<Double> location;
private Date dateCreated;
private Date dateModified;
public Product() {
}
public List<Double> getLocation() {
return location;
}
@ValidateOnExecution
@LocationIsValid
public List<Double> getLocation(String location) {
String[] locationString = location.split(";");
if (locationString.length != 2) {
return null;
}
List<Double>locations = new ArrayList<Double>();
for (int i = 0; i < locationString.length; i++) {
locations.add(Double.parseDouble(locationString[i]));
}
return locations;
}
// Other setters getters
}
产品资源
@GET
public ProductList getProducts(@QueryParam("near") String location) {
// parse the locations variable
Product product = new Product();
// did not work
// I have placed a breakpoint on the LocationIsValid
List<Double> locations = product.getLocation(location);
}
注意:我非常确定问题不在LocationIsValid中,因为当我将其验证为一个实体时,它可以正常工作@Valid Product product
我正在使用Jersey 2.4.1和Jersey bean validation 2.4.1依赖项
共 (0) 个答案