2015-05-04 55 views
0

我已經遍歷對象圖來拾取所有字段及其給定的註釋,並且想要基於註釋驗證從XSD構建的域對象。如何獲取@XmlElement註釋的值

但是,我被卡在@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; 

    } 

這一定是簡單的,但我想不出如何檢查所需的屬性被設置爲true,一旦我發現給定註釋類型@XmlElement的。

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

      // how to check value of required atrribute   
    } 
+0

這應該做的伎倆 http://stackoverflow.com/questions/4138754/getting-an-attribute-value-in -xml-element –

回答

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 
    } 
} 
+0

謝謝,這個伎倆 – John

相關問題