JAXB:@XmlTransient在第三方或外部超类上 [英] JAXB: @XmlTransient on third-party or external super class

查看:343
本文介绍了JAXB:@XmlTransient在第三方或外部超类上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于JAXB 2.1的以下问题,我需要一些帮助。

I need some help regarding the following issue with JAXB 2.1.

示例:我创建了一个扩展抽象类Person的SpecialPerson类。
现在我想使用JAXB将我的对象结构转换为XML模式。因此,我不希望Person XML类型出现在我的XML模式中以保持模式简单。相反,我希望Person类的字段显示在SpecialPerson XML类型中。

Sample: I've created a SpecialPerson class that extends a abstract class Person. Now I want to transform my object structure into a XML schema using JAXB. Thereby I don't want the Person XML type to appear in my XML schema to keep the schema simple. Instead I want the fields of the Person class to appear in the SpecialPerson XML type.

通常我会在类级别上将注释@XmlTransient添加到Person代码中。

Normally I would add the annotation @XmlTransient on class level into the Person code.

问题是Person是第三方类,我不可能在这里添加@XmlTransient。如何在没有注释类的情况下告诉JAXB它应该忽略Person类。是否有可能以某种方式从外部配置?

The problem is that Person is a third-party class and I have no possibility to add @XmlTransient here. How can I tell JAXB that it should ignore the Person class without annotating the class. Is it possible to configure this externally somehow?

您之前遇到过同样的问题吗?有什么想法,这个问题的最佳解决方案是什么?

Have you had the same problem before? Any ideas what the best solution for this problem would be?

推荐答案

好的,这是你知道什么的痛苦。最后,在筛选了许多博客帖子之后,这就是我所做的,

OK, this was a pain in the you-know-what. Finally, after sifting through many a blog postings, here's what I did,

在第三方类包中添加了一个package-info.java类,如下所示,

added a package-info.java class in the 'third-party class' package like this,

@ javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.NONE)
package third-party-package;

@javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.NONE) package third-party-package;

在我的情况下,它只是一个包,所以很容易。显然,您必须为每个单独的包结构执行此操作。我没有尝试在主程序包级别执行此操作。

In my case, it was just one package so it was easy. Obviously, you will have to do this for for every separate package structure. I haven't tried doing it at a master package level.

这篇关于JAXB:@XmlTransient在第三方或外部超类上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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