【问题标题】:How to get value of @XmlElement annotation如何获取@XmlElement 注释的值
【发布时间】:2015-05-04 10:36:30
【问题描述】:

我已经遍历对象图以获取所有字段及其给定的注释,并希望根据注释验证从 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         
    }

【问题讨论】:

标签: java validation reflection xsd annotations


【解决方案1】:

你可以这样实现:

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

【讨论】:

    猜你喜欢
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多