如何获取@XmlElement 注解的值

How to get value of @XmlElement annotation

我已经遍历了对象图以获取所有字段及其给定的注释,并希望根据注释验证从 XSD 构建的域对象。

但是,我卡在了@XmlElement 上,因为我不知道如何获取所需属性的值。

import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlElement;
    public class SomeClass {

     @XmlElement(name = "user_id", required = true)
     @NotNull
     protected String userId;

    }

这一定很简单,但是一旦我检测到给定注释的类型为@XmlElement,我就不知道如何检查所需的属性是否设置为 true。

    if(annotation.annotationType().equals(XmlElement.class)) {

            // how to check value of required atrribute         
    }

你可以这样实现:

// iterate over the fields in the required class. check if the annotatino is present
if (inputField.isAnnotationPresent(XmlElement.class)) {
    XmlElement xmlElementAnnotation = inputField.getAnnotation(XmlElement.class);

    // get 'required' value
    if(xmlElementAnnotation.required()) {
        // logic
    }
}