Jaxb:非法注释异常。

3

我遇到了以下异常:"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 Field,但是我仍然遇到了相同的异常。


我缺少默认构造函数。 public Image () { }


你所遇到的异常有什么堆栈跟踪信息? - bdoughan
1个回答

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接