【问题标题】:Reflection problems with super class in Spring BootSpring Boot中超类的反射问题
【发布时间】:2022-02-06 15:03:08
【问题描述】:

我的目标是创建类级别的注释,但我不断收到此错误。出于某种原因,当我使用 getClass 获取注释信息时,反射不起作用。我尝试使用方面,但我不确定如何使 spring boot 方面注释类级别。

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.lang.Class.

以下是我使用的类

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
public @interface ValidateInput {
    short minLength();
    short maxLength();
    String regex() default ""; 
}
public abstract class SimpleValidation {

    private String text;
    private ValidateInput info = this.getClass().getAnnotation(ValidateInput.class);

    protected SimpleValidation(String text) {
        this.text = text;   
    }
}
@Value
@EqualsAndHashCode(callSuper = true)
@ValidateInput(maxLength = 50, minLength = 5, regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$")
public class Email extends SimpleValidation {
    String email;

    public Email(String email) {
        super(email);
        this.email = email;
    }
}

【问题讨论】:

    标签: java spring mongodb spring-boot


    【解决方案1】:

    我认为您正在尝试重新创建已经是标准的 Bean 验证行为 try to use it,因为它比创建繁重的类层次结构来实现它更容易。

    Spring 将能够自动验证您的验证(在反序列化 JSON 主体之后,在插入 DB 之前..)

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 2020-06-26
      • 2018-06-01
      • 2020-11-26
      • 2018-02-05
      • 2021-06-28
      • 2021-05-16
      • 2018-07-03
      • 1970-01-01
      相关资源
      最近更新 更多