自定义参数验证注解 | java | java 技术论坛-江南app体育官方入口

接口传参,对象接收。其中有几个参数不能同时为null,请问如何通过一个作用在字段上的自定义注解来实现?

讨论数量: 3

我也不会,直接问的,gpt生成: 你可以通过自定义注解和一个自定义的校验器来实现对字段进行校验。以下是具体的实现步骤:

  1. 创建自定义注解

    import javax.validation.constraint;
    import javax.validation.payload;
    import java.lang.annotation.elementtype;
    import java.lang.annotation.retention;
    import java.lang.annotation.retentionpolicy;
    import java.lang.annotation.target;
    @target({elementtype.type})
    @retention(retentionpolicy.runtime)
    @constraint(validatedby = notallnullvalidator.class)
    public @interface notallnull {
        string message() default "fields cannot all be null";
        class<?>[] groups() default {};
        class<? extends payload>[] payload() default {};
        string[] fieldnames();
    }
  2. 创建自定义校验器

    import javax.validation.constraintvalidator;
    import javax.validation.constraintvalidatorcontext;
    import java.lang.reflect.field;
    public class notallnullvalidator implements constraintvalidator<notallnull, object> {
        private string[] fieldnames;
        @override
        public void initialize(notallnull constraintannotation) {
            this.fieldnames = constraintannotation.fieldnames();
        }
        @override
        public boolean isvalid(object value, constraintvalidatorcontext context) {
            if (value == null) {
                return true;
            }
            try {
                boolean allnull = true;
                for (string fieldname : fieldnames) {
                    field field = value.getclass().getdeclaredfield(fieldname);
                    field.setaccessible(true);
                    object fieldvalue = field.get(value);
                    if (fieldvalue != null) {
                        allnull = false;
                        break;
                    }
                }
                return !allnull;
            } catch (exception e) {
                e.printstacktrace();
                return false;
            }
        }
    }
  3. 在你的对象类上使用自定义注解

    import javax.validation.constraints.notnull;
    @notallnull(fieldnames = {"field1", "field2", "field3"}, message = "field1, field2, and field3 cannot all be null")
    public class myobject {
        private string field1;
        private string field2;
        private string field3;
        // getters and setters
        public string getfield1() {
            return field1;
        }
        public void setfield1(string field1) {
            this.field1 = field1;
        }
        public string getfield2() {
            return field2;
        }
        public void setfield2(string field2) {
            this.field2 = field2;
        }
        public string getfield3() {
            return field3;
        }
        public void setfield3(string field3) {
            this.field3 = field3;
        }
    }
  4. 在你的控制器中使用验证

    import org.springframework.validation.annotation.validated;
    import org.springframework.web.bind.annotation.postmapping;
    import org.springframework.web.bind.annotation.requestbody;
    import org.springframework.web.bind.annotation.restcontroller;
    import javax.validation.valid;
    @restcontroller
    @validated
    public class mycontroller {
        @postmapping("/validate")
        public string validate(@valid @requestbody myobject myobject) {
            return "validated successfully";
        }
    }

这样,通过使用 @notallnull 注解,你可以确保在进行参数校验时,指定的字段不能同时为 null。在对象的 @notallnull 注解中,fieldnames 属性指定了需要检查的字段名称,如果这些字段全部为 null,校验器将返回 false 并触发验证失败。

5个月前

好的,谢谢。我本来想着加到类的字段上来着。

5个月前

file

file

这样简单处理就行

5个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图