简体   繁体   English

动态应用JAXB批注

[英]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. 假设我有一个带有字段( idnameauthor )的BOOK.java文件,现在将其编组为XML文件时,我只希望将id写入XML文件,但这必须由某些条件决定。 So I was thinking to inject @XmlTransient annotations to other two fields on that condition at runtime. 所以我在考虑在运行时在这种情况下将@XmlTransient注释注入其他两个字段。 Is it possible to inject an JAXB annotations at runtime? 是否可以在运行时注入JAXB注释? Can i do that and if i can, how can i do so? 我可以这样做吗,如果可以,我该怎么做?

Is there any way other then using javassist to do so ? 除了使用javassist之外,还有其他方法吗?

It sounds like you just want something that's not JAXB. 听起来您只是想要一些非JAXB的东西。 JAXB is great when it fits, but it's pretty restrictive in what it allows you to do. 当适合时,JAXB很棒,但是在允许您执行的操作上有很大的限制。 Something like XStream would probably fit your needs better. XStream这样的东西可能会更适合您的需求。 You can write a custom Converter that will handle all the logic of whether to write a particular field or not. 您可以编写一个自定义的Converter ,它将处理是否编写特定字段的所有逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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