简体   繁体   中英

How to get value of @XmlElement annotation

I have made traversal through object graph to pick up all fields and their given annotations and would like to validate domain objects built from the XSDs based on the annotations.

However, i got stuck on the @XmlElement as i don't know how to get the value of required attribute.

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

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

    }

This must be simple but i cannot figure how to check if the attribute required is set to true once i detected that the given annotation is of type @XmlElement.

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

            // how to check value of required atrribute         
    }

You can achieve it this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM