【发布时间】:2019-09-21 13:07:05
【问题描述】:
我在我的 Java 应用程序中运行此代码,第一行抛出异常 IllegalAnnotationExceptions :
JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(productFound, serverOutput);
我不熟悉 XML 格式。因此,从我在互联网上看到的情况来看,问题来自于 Product 类,这意味着这个文件:
package service;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "Product")
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {
@XmlElement(name = "id")
private String id;
@XmlElement(name = "name")
private String name;
@XmlElement(name = "price")
private String price;
public Product(String id, String name, String price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
尤其是关于注释。 我做错什么了吗 ?还是在我的代码中看起来有些奇怪?
谢谢!
【问题讨论】: