简体   繁体   中英

Apply JAXB annotation dynamically

Suppose i have a BOOK.java file with the fields( id , name , author ) and now while marshalling it to XML file, I want only id to be written in XML file but that has to be decided by some condition. So I was thinking to inject @XmlTransient annotations to other two fields on that condition at runtime. Is it possible to inject an JAXB annotations at runtime? Can i do that and if i can, how can i do so?

Is there any way other then using javassist to do so ?

It sounds like you just want something that's not JAXB. JAXB is great when it fits, but it's pretty restrictive in what it allows you to do. Something like XStream would probably fit your needs better. You can write a custom Converter that will handle all the logic of whether to write a particular field or not.

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