java 编组时如何防止 JAXB 绑定 @XmlRootElement 的超类方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/659872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I prevent JAXB from binding superclass methods of the @XmlRootElement when marshalling?
提问by Matt
I have a class that is annotated as the @XmlRootElementwith @XmlAccessorType(XmlAccessType.NONE). The problem that I am having is that the superclass's methods are being bound, when I do not want them to be bound, and cannot update the class. I am hoping there is an annotation that I can put on the root element class to prevent this from happening.
我有一个被注释为@XmlRootElementwith 的类@XmlAccessorType(XmlAccessType.NONE)。我遇到的问题是超类的方法被绑定,当我不希望它们被绑定并且无法更新类时。我希望有一个注释可以放在根元素类上以防止这种情况发生。
Example:
例子:
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Person extends NamedObject {
@XmlElement
public String getId() { ... }
}
I would expect that only the methods annotated @XmlElementon Personwould be bound and marshalled, but the superclass's methods are all being bound, as well. The resulting XML then has too much information.
我希望,只有注释的方法@XmlElement上Person会被束缚和整理,但超类的方法都是约束,也是如此。生成的 XML 包含太多信息。
How do I prevent the superclass's methods from being bound without having to annotate the superclass, itself?
如何在不必注释超类本身的情况下防止超类的方法被绑定?
采纳答案by benvolioT
According to this StackOverflow post: How can I ignore a superclass?
根据这篇 StackOverflow 帖子: 如何忽略超类?
It is not possible with JAX-B to ignore the superclass without modifying the superclass. Quoting the relevant portion of that post:
JAX-B 不可能在不修改超类的情况下忽略超类。引用该帖子的相关部分:
Update2: I found a thread on java.netfor a similar problem. That thread resulted in an enhancement request, which was marked as a duplicate of another issue, which resulted in the @XmlTransient annotation. The comments on these bug reports lead me to believe this is impossible in the current spec.
更新 2:我在 java.net 上找到了一个线程来解决类似的问题。该线程导致了一个增强请求,该请求被标记为另一个问题的重复 ,这导致了 @XmlTransient 注释。对这些错误报告的评论让我相信这在当前规范中是不可能的。
回答by ivan_ivanovich_ivanoff
Just add
只需添加
@XmlAccessorType(XmlAccessType.NONE)
in front of EACH superclass declaration (and the class itself).
在每个超类声明(和类本身)之前。
In your case:
在你的情况下:
@XmlAccessorType(XmlAccessType.NONE)
class NamedObject{
[ ... ]
}
Remember that this has to be done really for each superclass, it is often forgotten when dealing with huge class dependency trees.
请记住,这必须真正为每个超类完成,在处理巨大的类依赖树时经常会忘记这一点。
Interfaces, of course, don't need any JAXB annotations.
当然,接口不需要任何 JAXB 注释。
回答by Zdenek F
I know this question is quite old, but there is a kind of solution which works if your superclass is in the same package as its child.
我知道这个问题已经很老了,但是如果您的超类与其子类位于同一个包中,则有一种解决方案有效。
Create a package-info.javain your package and insert
package-info.java在您的包中创建一个并插入
@XmlAccessorType(XmlAccessType.NONE)
package my.package.with.classes;
Obviously, it sets XmlAccessType.NONE upon all classes in the package. Myself, I use it in every package in our domain model. Therefore, I'm pretty safe. However, if your class is 'out of reach', f.e. it's in the JDK, use the solution from the accepted answer in [JAX-B] How can I ignore a superclass?.
显然,它在包中的所有类上设置 XmlAccessType.NONE。我自己,我在域模型的每个包中都使用它。因此,我很安全。但是,如果您的类“遥不可及”,如果它在 JDK 中,请使用[JAX-B] 如何忽略超类?.
I hope it's helpful for anynone who stumbles upon this question as I did.
我希望它对任何像我一样偶然发现这个问题的人有所帮助。
回答by Zdenek F
I'm facing the exact same problem. My superclass does not handle any JAXB annotations (it doesn't have to) and I would like my subclass not to include superclass properties while marshalling.
我面临着完全相同的问题。我的超类不处理任何 JAXB 注释(它不必),我希望我的子类在编组时不要包含超类属性。
Adding the XmlAccesorType on superclass cannot be the solution as I have no way to modify the superclass.
在超类上添加 XmlAccesorType 不是解决方案,因为我无法修改超类。
Is there any other solution?
还有其他解决方案吗?
回答by HDave
Replace your JAX-B implementation with MOXyand you can do anything you want. It has a ton of extensions that go above and beyond normal JAX-B, one of which will allow you to ignore inherited properties, etc. It also supports moving JAX-B annotations to an XML mapping file so you can keep multiple sets of mappings.
用MOXy替换你的 JAX-B 实现,你可以做任何你想做的事情。它有大量超出普通 JAX-B 的扩展,其中之一将允许您忽略继承的属性等。它还支持将 JAX-B 注释移动到 XML 映射文件,以便您可以保留多组映射.
回答by Nzall
A solution I have found, but which might not work for you depending on what you want to do, is to override the getters you want to ignore and let them return null. The JAXB specs, and by extension the implementations, ignore fields that contain a null value. Note that if you still need to be able to access the superclass value itself using the subclass, you may need to add a secondary accessor method that is not a getter and adjust your code accordingly.
我找到了一个解决方案,但根据您想要做什么,它可能对您不起作用,是覆盖您想要忽略的 getter 并让它们返回null。JAXB 规范以及扩展的实现忽略包含空值的字段。请注意,如果您仍然需要能够使用子类访问超类值本身,您可能需要添加一个不是 getter 的辅助访问器方法并相应地调整您的代码。

