xml – JAXB:IllegalAnnotationExceptions的2个计数
发布时间:2020-12-16 05:36:06 所属栏目:百科 来源:网络整理
导读:这是我的Parser类 public class Test { public static void main(String args[]) throws Exception { File file = new File("D:Test.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(MyOrder.class); Unmarshaller jaxbUnmarshaller = jaxbCont
这是我的Parser类
public class Test { public static void main(String args[]) throws Exception { File file = new File("D:Test.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(MyOrder.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); MyOrder customer = (MyOrder) jaxbUnmarshaller.unmarshal(file); System.out.println(customer.getOrder().getSide()); } } 这是MyOrder.java文件 @XmlRootElement(name = "BXML") public class MyOrder { @XmlElement(name = "Bag") protected Order order; public MyOrder() { } @XmlAttribute public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } } 这是我的域对象(Order.java) @XmlRootElement(name = "BXML") public class Order { public Order() { } @XmlAttribute(name = "Side") protected BigInteger Side; @XmlValue public BigInteger getSide() { return Side; } public void setSide(BigInteger side) { Side = side; } } 这是我试图运行程序时遇到的异常 Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions @XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML. this problem is related to the following location: at public com.Order com.MyOrder.getOrder() at com.MyOrder Class has two properties of the same name "order" this problem is related to the following location: at public com.Order com.MyOrder.getOrder() at com.MyOrder this problem is related to the following location: at protected com.Order com.MyOrder.order at com.MyOrder
对于@ XmlAttribute / @ XmlValue,需要引用映射到XML中的文本的Java类型.问题您需要将JAXBContext的初始化更改为以下内容:
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] {MyOrder.class,Order.class}); 对于Class有两个同名“order”问题的属性,需要更改受保护Order顺序的定义;私人订单; 此外,您希望将Order类的@XmlRootElement(name =“BXML”)更改为@XmlRootElement(name =“Order”). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |