【问题标题】:json validator in Java - using javax.validation.constraintsJava 中的 json 验证器 - 使用 javax.validation.constraints
【发布时间】:2021-02-25 08:53:32
【问题描述】:

我正在使用 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%? (valueB

【问题讨论】:

    标签: java json validation constraints


    【解决方案1】:

    有两种方法可以做到这一点:

    1. 您可以插入@AssertTrue 方法来验证它
        @AssertTrue
        public boolean isFiftyPercent(){
            //your logic to compare value a and value b
        }
    
    1. 或者您可以对全局设置进行自己的注释验证。见这里:https://www.baeldung.com/spring-mvc-custom-validator

    【讨论】:

      【解决方案2】:

      您正在寻找的是使用跨参数约束。一些基本指南可以在这里找到第 2.x 章 https://www.baeldung.com/javax-validation-method-constraints

      【讨论】:

      • 小提示:当你放一个链接的时候,你也会添加一个内容的小描述或者一些小例子,这样,在某些时候,链接不再有效,读者仍然需要考虑一些因素。
      【解决方案3】:

      为此,您需要有一个类级别的注释。字段级注解只访问字段的值。

      这是一个例子:

      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;
      }
      

      我尚未对此进行测试,但应该可以让您抢先一步。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多