json Java 中的验证器 - 使用 javax.validation.constraints
json validator in Java - using javax.validation.constraints
我正在使用 javax.validation.constraints 并且已经检查了包的使用情况,但仍然找不到我想做的事情。
https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/package-summary.html
这是从请求正文发送的两个变量
@NotNull
@PositiveOrZero
@Digits(integer = 9, fraction = 0)
private BigDecimal valueA;
@NotNull
@PositiveOrZero
@Digits(integer = 9, fraction = 0)
private BigDecimal valueB;
是否可以仅通过注释来限制valueB 不超过valueA 的50%? (值 B <= valueA/2)
您正在寻找的是使用交叉参数约束。可以在此处找到一些基本指南 2.x
https://www.baeldung.com/javax-validation-method-constraints
您需要对此进行 class 级注释。字段级注释仅访问字段的值。
这是一个例子:
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
// Custom annotation
@Target({ TYPE })
@Retention(RUNTIME)
@Repeatable(NotGreaterThans.class)
@Documented
@Constraint(validatedBy = { NotGreaterThanValidator.class }) // Explicitly define validator
public @interface NotGreaterThan {
String source();
String target();
double percentage()
String message() default "Default message ..";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({ TYPE })
@Retention(RUNTIME)
@Documented
@interface NotGreaterThans {
NotGreaterThan[] value();
}
}
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
// Validator accesses both annotation and object value
public class NotGreaterThanValidator implements ConstraintValidator<NotGreaterThan, Object> {
private String source;
private String target;
private double percentage;
@Override
public void initialize(NotGreaterThan notGreaterThan) {
this.source = notGreaterThan.source();
this.target = notGreaterThan.target();
this.percentage = notGreaterThan.percentage();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
BigDecimal sourceValue = customMethodToGetFieldValue(source, value);
BigDecimal targetValue = customMethodToGetFieldValue(target, value);
return source.compareTo(target.multiply(percentage)) <= 0;
}
private BigDecimal customMethodToGetFieldValue(String fieldName, Object object) {
return ....
}
}
// Define your annotation on type
@NotGreaterThan(source ="a", target="b", percentage =50.0)
public class MyCustomBodyClass {
private BigDecimal a;
private BigDecimal b;
}
我还没有对此进行测试,但应该可以让您先行一步。
有两种方法可以做到这一点:
- 你可以插入@AssertTrue 方法来验证它
@AssertTrue
public boolean isFiftyPercent(){
//your logic to compare value a and value b
}
- 或者您可以为全局设置进行自己的注释验证。看这里:https://www.baeldung.com/spring-mvc-custom-validator
我正在使用 javax.validation.constraints 并且已经检查了包的使用情况,但仍然找不到我想做的事情。 https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/package-summary.html
这是从请求正文发送的两个变量
@NotNull
@PositiveOrZero
@Digits(integer = 9, fraction = 0)
private BigDecimal valueA;
@NotNull
@PositiveOrZero
@Digits(integer = 9, fraction = 0)
private BigDecimal valueB;
是否可以仅通过注释来限制valueB 不超过valueA 的50%? (值 B <= valueA/2)
您正在寻找的是使用交叉参数约束。可以在此处找到一些基本指南 2.x https://www.baeldung.com/javax-validation-method-constraints
您需要对此进行 class 级注释。字段级注释仅访问字段的值。
这是一个例子:
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
// Custom annotation
@Target({ TYPE })
@Retention(RUNTIME)
@Repeatable(NotGreaterThans.class)
@Documented
@Constraint(validatedBy = { NotGreaterThanValidator.class }) // Explicitly define validator
public @interface NotGreaterThan {
String source();
String target();
double percentage()
String message() default "Default message ..";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({ TYPE })
@Retention(RUNTIME)
@Documented
@interface NotGreaterThans {
NotGreaterThan[] value();
}
}
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
// Validator accesses both annotation and object value
public class NotGreaterThanValidator implements ConstraintValidator<NotGreaterThan, Object> {
private String source;
private String target;
private double percentage;
@Override
public void initialize(NotGreaterThan notGreaterThan) {
this.source = notGreaterThan.source();
this.target = notGreaterThan.target();
this.percentage = notGreaterThan.percentage();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
BigDecimal sourceValue = customMethodToGetFieldValue(source, value);
BigDecimal targetValue = customMethodToGetFieldValue(target, value);
return source.compareTo(target.multiply(percentage)) <= 0;
}
private BigDecimal customMethodToGetFieldValue(String fieldName, Object object) {
return ....
}
}
// Define your annotation on type
@NotGreaterThan(source ="a", target="b", percentage =50.0)
public class MyCustomBodyClass {
private BigDecimal a;
private BigDecimal b;
}
我还没有对此进行测试,但应该可以让您先行一步。
有两种方法可以做到这一点:
- 你可以插入@AssertTrue 方法来验证它
@AssertTrue
public boolean isFiftyPercent(){
//your logic to compare value a and value b
}
- 或者您可以为全局设置进行自己的注释验证。看这里:https://www.baeldung.com/spring-mvc-custom-validator