Hibernate Validator 方法或构造函数验证
Hibernate Validator method or constructor validation
如何使用 Hibernate 验证器来验证构造函数或方法中的参数?我希望在创建 ValueObject 之前进行验证,这样我就可以抛出异常而不创建对象,除非所有参数都有效。
基本上,如果可能的话,我正在尝试使用注释而不是做这样的事情:
public class ConditionalPerson {
private String name;
private String surname;
private int age;
public ConditionalPerson(String name, String surname, int age){
if (name == null || surname == null || age < 1) {
throw new IllegalArgumentException();
}
this.name = name;
this.surname = surname;
this.age = age;
}
}
我试过像这样遵循 docs 这似乎有效,但仍然会导致创建对象。
public class Person {
@NotNull(message = "Name can't be null")
@NotEmpty(message = "Name can't be empty")
@Length(min=1)
private String name;
@NotNull(message = "Surname can't be null")
@NotEmpty(message = "Surname can't be empty")
@Length(min=1)
private String surname;
@Range(min=100, max=200)
private int age;
public Person(String name, String surname, int age){
this.name = name;
this.surname = surname;
this.age = age;
}
}
给构造函数参数添加注释似乎没有效果
public Person(@NotNull String name,
@NotNull String surname,
@Range(min=100, max=200) int age) {
...
}
我是如何创建对象的:
public class Example {
Person person;
ConditionalPerson person2;
public static void main(String[] args) {
Example example = new Example();
example.makePerson();
example.makeConditionalPerson();
}
public void makePerson() {
person = new Person(null, "", 12);
Validator validator = ValidatorSingleton.getValidator();
Set<ConstraintViolation<Person>> violations = validator.validate(person);
if (violations.size() > 0) {
throw new IllegalArgumentException();
}
}
public void makeConditionalPerson() {
person2 = new ConditionalPerson(null, "", 123);
}
}
验证者:
public class ValidatorSingleton {
private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
private static final javax.validation.Validator validator = factory.getValidator();
private ValidatorSingleton(){}
public static Validator getValidator() {
return validator;
}
}
对于找到此 post 的任何其他人。
我稍微改变了我的方法并使用 OVal Validation & AspectJ 而不是 Hibernate 使它工作。
除了我需要在 class:
上方添加 @Guarded
之外,基本上与上面的示例相同
@Guarded
public class Person {
private String name;
private String surname;
private int age;
public Person(@NotNull String name, @NotNull String surname, @Range(min=100, max=200) int age){
this.name = name;
this.surname = surname;
this.age = age;
}
}
然后在你的 build.gradle 添加:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.aspectj:aspectjtools:1.8.10'
}
}
dependencies {
compile 'org.aspectj:aspectjrt:1.8.1'
compile 'net.sf.oval:oval:1.86'
}
tasks.withType(JavaCompile) {
doLast {
String[] args = ["-showWeaveInfo",
"-1.8",
"-inpath", destinationDir.toString(),
"-aspectpath", classpath.asPath,
"-d", destinationDir.toString(),
"-classpath", classpath.asPath]
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler)
}
您可以使用 hibernate 验证器执行此操作,使用反射来验证参数:
public class PersonTest {
private static ExecutableValidator executableValidator;
@BeforeClass
public static void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
executableValidator = factory.getValidator().forExecutables();
}
@Test
public void test() throws NoSuchMethodException {
Constructor<Person> constructor =
Person.class.getConstructor(String.class, String.class, int.class);
Set<ConstraintViolation<Person>> violations =
executableValidator.validateConstructorParameters(constructor, new Object[]{null, "", 12});
assertEquals(2, violations.size());
}
}
如何使用 Hibernate 验证器来验证构造函数或方法中的参数?我希望在创建 ValueObject 之前进行验证,这样我就可以抛出异常而不创建对象,除非所有参数都有效。
基本上,如果可能的话,我正在尝试使用注释而不是做这样的事情:
public class ConditionalPerson {
private String name;
private String surname;
private int age;
public ConditionalPerson(String name, String surname, int age){
if (name == null || surname == null || age < 1) {
throw new IllegalArgumentException();
}
this.name = name;
this.surname = surname;
this.age = age;
}
}
我试过像这样遵循 docs 这似乎有效,但仍然会导致创建对象。
public class Person {
@NotNull(message = "Name can't be null")
@NotEmpty(message = "Name can't be empty")
@Length(min=1)
private String name;
@NotNull(message = "Surname can't be null")
@NotEmpty(message = "Surname can't be empty")
@Length(min=1)
private String surname;
@Range(min=100, max=200)
private int age;
public Person(String name, String surname, int age){
this.name = name;
this.surname = surname;
this.age = age;
}
}
给构造函数参数添加注释似乎没有效果
public Person(@NotNull String name,
@NotNull String surname,
@Range(min=100, max=200) int age) {
...
}
我是如何创建对象的:
public class Example {
Person person;
ConditionalPerson person2;
public static void main(String[] args) {
Example example = new Example();
example.makePerson();
example.makeConditionalPerson();
}
public void makePerson() {
person = new Person(null, "", 12);
Validator validator = ValidatorSingleton.getValidator();
Set<ConstraintViolation<Person>> violations = validator.validate(person);
if (violations.size() > 0) {
throw new IllegalArgumentException();
}
}
public void makeConditionalPerson() {
person2 = new ConditionalPerson(null, "", 123);
}
}
验证者:
public class ValidatorSingleton {
private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
private static final javax.validation.Validator validator = factory.getValidator();
private ValidatorSingleton(){}
public static Validator getValidator() {
return validator;
}
}
对于找到此 post 的任何其他人。 我稍微改变了我的方法并使用 OVal Validation & AspectJ 而不是 Hibernate 使它工作。
除了我需要在 class:
上方添加@Guarded
之外,基本上与上面的示例相同
@Guarded
public class Person {
private String name;
private String surname;
private int age;
public Person(@NotNull String name, @NotNull String surname, @Range(min=100, max=200) int age){
this.name = name;
this.surname = surname;
this.age = age;
}
}
然后在你的 build.gradle 添加:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.aspectj:aspectjtools:1.8.10'
}
}
dependencies {
compile 'org.aspectj:aspectjrt:1.8.1'
compile 'net.sf.oval:oval:1.86'
}
tasks.withType(JavaCompile) {
doLast {
String[] args = ["-showWeaveInfo",
"-1.8",
"-inpath", destinationDir.toString(),
"-aspectpath", classpath.asPath,
"-d", destinationDir.toString(),
"-classpath", classpath.asPath]
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler)
}
您可以使用 hibernate 验证器执行此操作,使用反射来验证参数:
public class PersonTest {
private static ExecutableValidator executableValidator;
@BeforeClass
public static void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
executableValidator = factory.getValidator().forExecutables();
}
@Test
public void test() throws NoSuchMethodException {
Constructor<Person> constructor =
Person.class.getConstructor(String.class, String.class, int.class);
Set<ConstraintViolation<Person>> violations =
executableValidator.validateConstructorParameters(constructor, new Object[]{null, "", 12});
assertEquals(2, violations.size());
}
}