我收到以下异常" 1个IllegalAnnotationExceptions
"
代码:
Image image = new Image("url");
StringWriter sw = new StringWriter();
JAXBContext jaxbContext = JAXBContext.newInstance(Image.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(image, sw);
类别:
@XmlRootElement(name="ProductImage")
public class Image {
private String url;
public Image( String url) {
this.url = url;
}
@XmlElement(name = "ImageLocation")
public String getUrl() {
return this.url;
}
}
我尝试在字段上设置@XmlElement
注释并在类上设置AccessorType字段。但是我得到了同样的例外。
我错过了默认构造函数。 公众形象 () { }
答案 0 :(得分:2)
使用此类..
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="ProductImage")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "ProductImage", propOrder = {
"url"
})
public class Image {
public Image(){}
private String url;
public Image( String url) {
this.url = url;
}
@XmlElement(name = "ImageLocation")
public String getUrl() {
return this.url;
}
}
生成的XML是
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProductImage>
<ImageLocation>url</ImageLocation>
</ProductImage>