xml JAXB:2 次 IllegalAnnotationExceptions

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9991506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:17:34  来源:igfitidea点击:

JAXB : 2 counts of IllegalAnnotationExceptions

xmljaxb

提问by Pawan

This is my Parser class

这是我的解析器类

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());
    }
}

This is MyOrder.java file

这是 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;
    }
}

This is my Domain Object (Order.java )

这是我的域对象(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;
    }
}

This is the exception i am getting when i tried to run the program

这是我尝试运行程序时遇到的异常

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

回答by Brant Olsen

For the @XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.issue you need to change your initialization of JAXBContextto the following:

对于这个@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.问题,您需要将您的初始化更改JAXBContext为以下内容:

JAXBContext jaxbContext = JAXBContext.newInstance(MyOrder.class, Order.class);

For the Class has two properties of the same name "order"issue, you need to change the definition of protected Order order;to private Order order;.

对于该Class has two properties of the same name "order"问题,您需要将 的定义更改protected Order order;private Order order;

Also, you want to change the @XmlRootElement(name = "BXML")of your Orderclass to @XmlRootElement(name = "Order").

此外,你想改变@XmlRootElement(name = "BXML")你的Order@XmlRootElement(name = "Order")

回答by Ashish Jha

You can see the below sample code to generate Java Object from given XML.It is working fine in my system.

你可以看到下面的示例代码从给定的 XML 生成 Java 对象。它在我的系统中工作正常。

customer.xml

客户.xml

<?xml version="1.0" encoding="UTF-8"?>
<company>
    <customer id="100">
        <age>25</age>
        <name>Ram</name>
        <Address>
            <city>Bangalore</city>
            <country>India</country>
        </Address>
        <Address>
            <city>Patna</city>
            <country>India</country>
        </Address>
    </customer>

    <customer id="200">
        <age>26</age>
        <name>Ashu</name>
        <Address>
            <city>Delhi</city>
            <country>India</country>
        </Address>
        <Address>
            <city>Madhubani</city>
            <country>India</country>
        </Address>
    </customer>
</company>

Company.java

公司.java

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="company")
public class Company {

    @XmlElement(name="customer")
    private List<Costumer> custList;
    //

    public List<Costumer> getCustList() {
        return custList;
    }

    public void setCustList(List<Costumer> custList) {
        this.custList = custList;
    }
    //

    @Override
    public String toString() {
        return "Company [custList=" + custList + "]";
    }
}

Costumer.java

客户.java

@XmlAccessorType(XmlAccessType.FIELD)
class Costumer {
    @XmlElement(name="name")
    private String name;

    @XmlElement(name="age")
    private int age;

    @XmlElement(name="id")
    private int id;

    @XmlElement(name="Address")
    private List<Address> addressList;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Address> getAddressList() {
        return addressList;
    }

    public void setAddressList(List<Address> addressList) {
        this.addressList = addressList;
    }

    @Override
    public String toString() {
        return "Customer [name=" + name + ", age=" + age + ", id=" + id + ", addressList=" + addressList + "]";
    }
}

Address.java

地址.java

@XmlAccessorType(XmlAccessType.FIELD)
class Address {
    @XmlElement(name="city")
    private String city;

    @XmlElement(name="country")
    private String country;
    //
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
    //
    @Override
    public String toString() {
        return "Address [city=" + city + ", country=" + country + "]";
    }
}

TestMain.java

测试主程序

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class TestMain {

    public static void main(String[] args) {

        String xmlPath = "C:\" + File.separator  + "customer.xml";

        try {

            File file = new File(xmlPath);

            JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] {Company.class,Address.class,Costumer.class});
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Company customer = (Company) jaxbUnmarshaller.unmarshal(file);
            System.out.println(customer);

          } catch (JAXBException e) {
            e.printStackTrace();
          }
    }
}
Outout:
Company [custList=[Customer [name=Ram, age=25, id=0, addressList=[Address [city=Bangalore, country=India], Address [city=Patna, country=India]]], Customer [name=Ashu, age=26, id=0, addressList=[Address [city=Delhi, country=India], Address [city=Madhubani, country=India]]]]]

回答by BinDev

This is because the sub-elements of that class you are creating JAXBcontext instance ,doesn't have the same name as of the element names defined inside it.

这是因为您正在创建 JAXBcontext 实例的该类的子元素与其中定义的元素名称不具有相同的名称。

Example:

例子:

@XmlType(name = "xyz", propOrder = { "a", "b", "c", "d" })
@XmlRootElement(name = "testClass")
public class TestClass
{

  @XmlElement(required = true)
  protected Status status;
  @XmlElement(required = true)
  protected String mno;
  @XmlElement(required = true)
}

In the above class you don't have "xyz" , but if you will put the property name that is not available JAXBContext instantiation throws IlligalAnnotationException.

在上面的类中,您没有 "xyz" ,但是如果您将放置不可用的属性名称,则 JAXBContext 实例化会抛出 IlligalAnnotationException。

回答by Snickbrack

If anyone is curious about the usage of JAXB and Lombok.

如果有人对 JAXB 和 Lombok 的使用感到好奇。

My fix was to remove the getter and setter from the root object.

我的解决方法是从根对象中删除 getter 和 setter。