简体   繁体   中英

JAXB - adapter for a library object

I want to read an integer (inside XML element) to a Semaphore instead of int, effectively calling Semaphore(int theInteger) . Problem is - Semaphore doesn't have a default constructor. If it was a class I wrote I could either make a no-arg private constructor or write an adapter, but since I have no access to Semaphore - what can I do?

An object that you are adapting with an XmlAdapter doesn't need to a no-art constructor. You could set it up as follows:

SemaphoreAdapter extends XmlAdapter<Integer, Semaphore>

Then you would use the @XmlJavaTypeAdapter annotation on the property of type Semaphore to reference the XmlAdapter .

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