Reputation: 845
I am getting the following exception "1 counts of IllegalAnnotationExceptions
"
Code:
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;
}
}
I tried setting the @XmlElement
annotation on the field and setting AccessorType Field on the class. But I am getting the same exception.
I was missing the default constructor. public Image () { }
Upvotes: 3
Views: 2111
Reputation: 8282
Use this 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 generated is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProductImage>
<ImageLocation>url</ImageLocation>
</ProductImage>
Upvotes: 2