JAXB中的java.awt.Point和java.awt.Rectangle [英] java.awt.Point and java.awt.Rectangle in JAXB
问题描述
我想我可能会尝试JAXB来处理存储和恢复设置。但即使使用最简单的例子,我也遇到了麻烦:
I thought I might give JAXB a try to take care of storing and restoring settings. But I ran into trouble even with the "simplest" example:
import java.awt.Point;
public class Config {
public Point myPoint = new Point();
}
从我试过的代码中的其他地方开始:
And from somewhere else in the code I tried:
JAXB.marshal(new Config(), new File("config.xml"));
这会产生一些非常长的堆栈跟踪异常......
This produces some exception with a very long stack trace...
这是否意味着,我需要在使用JAXB之前为x,y编写一个包装类?
Does that mean, i need to write a wrapper class for x,y before I can use JAXB?
推荐答案
要编组的对象必须注释为JAXB对象。根据JAXB实现,这也可以通过外部映射文件来实现,例如 MOXy 。
The object(s) to be marshalled must be annotated as JAXB object(s) indeed. Depending on the JAXB implementation, this can also be achieved by external mapping files like in the case of MOXy.
你不必写一个包装类,你只需要添加现有类所需的注释,如 @XmlRootElement
。但是,对于您无法控制的类(如 java.awt.Point
),您将使用 @XmlJavaTypeAdapter
,其中一个例子可以在这里找到。
You don't have to write a wrapper class, you can just add the required annotations like @XmlRootElement
to your existing classes. However, in the case of classes you don't have control over (like java.awt.Point
), you would use an @XmlJavaTypeAdapter
, an example of which can be found here.
这篇关于JAXB中的java.awt.Point和java.awt.Rectangle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!