编组时如何防止 JAXB 绑定 @XmlRootElement 的超类方法? [英] How do I prevent JAXB from binding superclass methods of the @XmlRootElement when marshalling?

查看:28
本文介绍了编组时如何防止 JAXB 绑定 @XmlRootElement 的超类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 @XmlAccessorType(XmlAccessType.NONE) 注释为 @XmlRootElement 的类.我遇到的问题是超类的方法被绑定,当我不希望它们被绑定并且无法更新类时.我希望有一个注释可以放在根元素类上以防止这种情况发生.

I have a class that is annotated as the @XmlRootElement with @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.

示例:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Person extends NamedObject {

    @XmlElement
    public String getId() { ... }

}

我希望只有在 Person 上注释了 @XmlElement 的方法会被绑定和编组,但超类的方法也都被绑定.结果 XML 包含太多信息.

I would expect that only the methods annotated @XmlElement on Person would be bound and marshalled, but the superclass's methods are all being bound, as well. The resulting XML then has too much information.

如何在不必注释超类本身的情况下防止超类的方法被绑定?

How do I prevent the superclass's methods from being bound without having to annotate the superclass, itself?

推荐答案

根据这篇 StackOverflow 帖子:如何忽略超类?

According to this StackOverflow post: How can I ignore a superclass?

JAX-B 不可能在不修改超类的情况下忽略超类.引用该帖子的相关部分:

It is not possible with JAX-B to ignore the superclass without modifying the superclass. Quoting the relevant portion of that post:

更新 2:我在 java.net 上找到了一个线程对于类似的问题.那个线程导致了增强请求,被标记为重复的另一个问题,导致@XmlTransient 注释.评论在这些错误报告中,我发现相信这是不可能的当前规格.

Update2: I found a thread on java.net for 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.

这篇关于编组时如何防止 JAXB 绑定 @XmlRootElement 的超类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆