Jaxb:非法注解异常
Jaxb : IllegalAnnotationExceptions
我收到以下异常“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);
Class:
@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
注释并在 class 上设置 AccessorType 字段。但我遇到了同样的异常。
我缺少默认构造函数。
public 图片 () {
}
使用这个Class..
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>
我收到以下异常“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);
Class:
@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
注释并在 class 上设置 AccessorType 字段。但我遇到了同样的异常。
我缺少默认构造函数。 public 图片 () { }
使用这个Class..
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>