jaxb问题-非法的反射访问操作

时间:2019-04-20 02:04:19

标签: java jaxb

穿越和非法的相对访问操作

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/Users/sshams/.m2/repository/com/sun/xml/bind/jaxb-impl/2.2.11/jaxb-impl-2.2.11.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release Exception in thread "main"
javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.

客户分类

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(propOrder = {"name", "phone", "about", "age", "balance", "active", "joined"})
public class Customer {

    private int id;
    private String name;
    private String phone;
    private String about;
    private int age;
    private BigDecimal balance;
    private boolean active;
    private Date joined;

    public Customer() {
    }

    @XmlAttribute
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public int getAge() {
        return age;
    }

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

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Date getJoined() {
        return joined;
    }

    public void setJoined(Date joined) {
        this.joined = joined;
    }
}

客户分类

@XmlRootElement(name = "customers")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Customers {

    @XmlElement(name="customer", type=Customer.class)
    private List<Customer> customers;

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }
}

主班

public static void main(String[] args) throws IOException, ParserConfigurationException, JAXBException {
    List<Customer> customers = DataProvider.readDataFromXML("data/customers.xml");

    JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class);
    Marshaller marshaller = jaxbContext.createMarshaller();

    StringWriter stringWriter = new StringWriter();
    marshaller.marshal(customers2, stringWriter);

    System.out.println(stringWriter.toString());
}

0 个答案:

没有答案