本文介绍了验证可选字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下请求json

I have a following request json

{
 "ParameterA":"value",
 "ParameterB":[
  {
    "key":"",
  "value":""
  },
  {
    "key":"",
  "value":""
  }
    ]
}

我正在尝试验证此json,我的要求是,如果存在ParameterB,则可以确定里面有一个列表shd,否则ParameterB是可选的.

I am trying to validate this json , my requirement is as such that if ParameterB is present then a list shd be present inside for sure, else ParameterB is optional.Such as if ParameterB itself only not present there shd nt be a problem.

我正在寻找相同的Java验证注释.我已经在键和值上使用@NotNull,但是由于它是存在列表的数组,因此无法决定在ParameterB上使用什么

I am looking for an java validation annotation for the same.i have used @NotNull on key and value but not able to decide what to use on ParameterB since it is the array inside which list is present

由于它也具有数据库连接性,因此我正在使用JPA并进行此REST调用.

Since it has a database connectivity as well , I am using JPA and making this REST call.

我在ParameterB上尝试了@NotEmpty,但这没有帮助.我想要一个注释,以便如果不存在ParameterB本身,则会影响实际流程.跟随案例

I tried @NotEmpty on ParameterB but that doesnt help. I want an annotation so that if ParameterB itself is not present it shd nt affect the actual flow. Following Case

{
 "ParameterA":"value"
}

任何建议或帮助将不胜感激.

any suggestion or help will be appreciated.

推荐答案

您要查找的验证是非标准验证.我不知道实现这种验证的现有注释.

The validation your are looking for is non standard one. I don't know about the existing annotation that implements this kind of validation.

幸运的是,使用自定义验证规则可以轻松扩展 javax.validation 约束:

Fortunately for you javax.validation constraints can be easily extended with custom validation rules:

a)创建约束注释

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

@Target({METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = NullOrNotEmptyValidator.class)
@Documented
public @interface NullOrNotEmpty {

    String message() default "{com.mycompany.constraints.nullornotempty}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}

b)实施验证器

import java.lang.reflect.Array;
import java.util.Map;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class NullOrNotEmptyValidator implements ConstraintValidator<NullOrNotEmpty, Object> {

    @Override
    public void initialize(NullOrNotEmpty constraintAnnotation) {
    }

    @Override
    public boolean isValid(Object object, ConstraintValidatorContext constraintContext) {

        if (object == null) {
            return true;
        }

        final Class<?> fieldClass = object.getClass();
        if (fieldClass.isArray()) {
            return Array.getLength(object) > 0;
        }

        if (Iterable.class.isAssignableFrom(fieldClass)) {
            return ((Iterable) object).iterator().hasNext();
        }

        if (Map.class.isAssignableFrom(fieldClass)) {
            return !((Map) object).isEmpty();
        }

        return false;
    }

}

c)定义默认错误消息

c) Define a default error message

带有示例的Hibernate Validator文章:第3章.创建自定义约束

Article with examples for Hibernate Validator: Chapter 3. Creating custom constraints

这篇关于验证可选字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 10:04