我试图用jaxb编组一个包含Image的对象,然后解组它(即保存/加载)。
有没有办法存储该图片?
我正在尝试创建一个返回描述swt.image imagedata的字节数组的函数,但是一旦我将其标记为@XmlElement,存储它的过程就会失败抛出这样的异常:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
JAXB annotation is placed on a method that is not a JAXB property
this problem is related to the following location:
at @javax.xml.bind.annotation.XmlElement()
另外,我已经测试过将SWT.Image转换为AWT.BufferedImage,但我仍然得到相同的异常。
答案 0 :(得分:2)
您的异常表示您已在不是访问者的方法(get / set方法)上放置了注释。以下是使用java.awt.Image
属性的示例:
<强>根强>
package forum9094655;
import java.awt.Image;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Root {
private Image image;
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
}
<强>演示强>
package forum9094655;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
Image image = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
root.setImage(image);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
<强>输出强>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<image>iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mNgYGAAAAAEAAHI6uv5AAAAAElFTkSuQmCC</image>
</root>