(三)声明和验证方法约束

首发博客地址

https://blog.zysicyj.top/

一、声明方法约束

package org.hibernate.validator.referenceguide.chapter03.parameter;
public class RentalStation {
    public RentalStation(@NotNull String name) {
        //...
    }
    public void rentCar(
            @NotNull Customer customer,
            @NotNull @Future Date startDate,
            @Min(1) int durationInDays) {
        //...
    }
}
  1. RentalStation的name不能为空

  2. rentCar(),customer不能为空,startDate不能为空且必须在未来,durationInDays最少为1天

1. 交叉参数约束

public class Car {
    @LuggageCountMatchesPassengerCount(piecesOfLuggagePerPassenger = 2)
    public void load(List<Person> passengers, List<PieceOfLuggage> luggage) {
        //...
    }
}

该注解通过自动义确保乘客携带不超过2件行李

2. 返回值约束

package org.hibernate.validator.referenceguide.chapter03.returnvalue;
public class RentalStation {
    @ValidRentalStation
    public RentalStation() {
        //...
    }
    @NotNull
    @Size(min = 1)
    public List<@NotNull Customer> getCustomers() {
        //...
        return null;
    }
}
  • 任何新创建的RentalStation对象必须满足@ValidRentalStation约束

  • 传回的客户清单getCustomers()不得为null并且必须至少包含元素

  • 返回的客户清单getCustomers()不得包含任何null对象

3. 级联验证

package org.hibernate.validator.referenceguide.chapter03.cascaded;
public class Garage {
    @NotNull
    private String name;
    @Valid
    public Garage(String name) {
        this.name = name;
    }
    public boolean checkCar(@Valid @NotNull Car car) {
        //...
        return false;
    }
}
package org.hibernate.validator.referenceguide.chapter03.cascaded;
public class Car {
    @NotNull
    private String manufacturer;
    @NotNull
    @Size(min = 2, max = 14)
    private String licensePlate;
    public Car(String manufacturer, String licencePlate) {
        this.manufacturer = manufacturer;
        this.licensePlate = licencePlate;
    }
    //getters and setters ...
}
  1. 验证checkCar()方法的参数时,还将Car评估对所传递对象的属性的约束。同样,在验证构造函数的返回值时@NotNull,Garage将检查对name字段的约束 Garage。

  2. 通常,级联验证对可执行文件的工作方式与对JavaBeans属性完全相同。

  3. 特别是,null在级联验证期间会忽略值,并且递归执行级联验证

package org.hibernate.validator.referenceguide.chapter03.cascaded.containerelement;
public class Garage {
    public boolean checkCars(@NotNull List<@Valid Car> cars) {
        //...
        return false;
    }
}

4. 继承层次结构中的方法约束

  • 需要注意的规则

    • 方法的调用者必须满足的前提条件不能在子类型中得到加强

    • 保证方法调用者的后置条件不会在子类型中减弱

  • 子类非法的方法参数约束

package org.hibernate.validator.referenceguide.chapter03.inheritance.parameter;
public interface Vehicle {
    void drive(@Max(75) int speedInMph);
}
package org.hibernate.validator.referenceguide.chapter03.inheritance.parameter;
public class Car implements Vehicle {
    @Override
    public void drive(@Max(55) int speedInMph) {
        //...
    }
}
  • 次结构的并行类型中的非法方法参数约束

package org.hibernate.validator.referenceguide.chapter03.inheritance.parallel;
public interface Vehicle {
    void drive(@Max(75) int speedInMph);
}
package org.hibernate.validator.referenceguide.chapter03.inheritance.parallel;
public interface Car {
    void drive(int speedInMph);
}
package org.hibernate.validator.referenceguide.chapter03.inheritance.parallel;
public class RacingCar implements Car, Vehicle {
    @Override
    public void drive(int speedInMph) {
        //...
    }
}
  • 父类型和子类型方法的返回值约束

package org.hibernate.validator.referenceguide.chapter03.inheritance.returnvalue;
public interface Vehicle {
    @NotNull
    List<Person> getPassengers();
}
package org.hibernate.validator.referenceguide.chapter03.inheritance.returnvalue;
public class Car implements Vehicle {
    @Override
    @Size(min = 1)
    public List<Person> getPassengers() {
        //...
        return null;
    }
}

二、验证方法约束

再来一辆车

class Car2 {
    public Car2(@NotNull String manufacturer) {
    }
    @自定义规则
    public Car2() {
    }
    public void drive(@Max(75) @Min(22) int speedInMph) {
    }
    @Size(min = 1)
    public List getParams() {
        return null;
    }
}

获取工具

1. 校验普通方法参数

2. 测试普通方法返回值

3. 测试构造方法参数

4. 测试构造方法的返回值

三、内置方法约束

package org.hibernate.validator.referenceguide.chapter03.parameterscriptassert;
public class Car {
    @ParameterScriptAssert(lang = "javascript", script = "luggage.size() <= passengers.size() * 2")
    public void load(List<Person> passengers, List<PieceOfLuggage> luggage) {
        //...
    }
}
```        //...
    }
}

最后更新于