簡體   English   中英

此上下文已知javax.xml.bind.MarshalException或其任何超類

[英]javax.xml.bind.MarshalException nor any of its super class is known to this context

我正在超越例外,正在尋找解決方案,任何幫助將不勝感激。 在其他一些郵件中發現了相同的問題,但它們對我不起作用。 請參見下面的代碼。

javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.SAXException2: class com.mycompany.soma.ws.rest.v1.model.test.EmployeeConstruction nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class com.mycompany.soma.ws.rest.v1.model.test.EmployeeConstruction nor any of its super class is known to this context.]
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:326)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
    at com.mycompany.soma.ws.rest.v1.model.test.Test.main(Test.java:39)

<snapshots>
    <employees>
        <employee>
            <name>Dan</name>
        </employee>
        <employee>
            <name>Samy</name>
        </employee>
    </employees>
</shapshots>

    package com.mycompany.soma.ws.rest.v1.model.test;

import java.util.ArrayList;
import java.util.List;

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

public class Test {
    public static void main(String[] args) {

        EmployeeConstruction ec = new EmployeeConstruction();
        ec.setName("Construction employee");
        ec.setSomeContrction("construction bulding");

        EmployeesElement<EmployeeConstruction> ee = new EmployeesElement<EmployeeConstruction>();

        List<EmployeeConstruction> list = new ArrayList<EmployeeConstruction>();
        list.add(ec);
        ee.setEmployees(list);

        Snapshots<EmployeeConstruction> snapshots = new Snapshots<EmployeeConstruction>();
        snapshots.setEmployeesElement(ee);

        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance(Snapshots.class);

            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(snapshots, System.out);

        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


package com.mycompany.soma.ws.rest.v1.model.test;

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;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"employeesElement"})
@XmlRootElement(name = "snapshots")
public class Snapshots<T> {

    @XmlElement(name = "employees")
    private EmployeesElement<T> employeesElement;


    public EmployeesElement<T> getEmployeesElement() {
        return employeesElement;
    }

    public void setEmployeesElement(EmployeesElement<T> employeesElement) {
        this.employeesElement = employeesElement;
    }
}



package com.mycompany.soma.ws.rest.v1.model.test;

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;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "employees")
public class EmployeesElement<T> {
    @XmlElement(name = "employees", nillable = true, required = false)
    List<T> employees;

    public List<T> getEmployees() {
        return employees;
    }

    public void setEmployees(List<T> employees) {
        this.employees = employees;
    }

}


package com.mycompany.soma.ws.rest.v1.model.test;

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.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "wsemployee")
@XmlType(name = "", propOrder = {"name"})
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({EmployeeConstruction.class, EmployeeManager.class})
public class Employee {
    @XmlElement(required = true)
    protected String name;

    public String getName() {
        return name;
    }

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


}


package com.mycompany.soma.ws.rest.v1.model.test;

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;
import javax.xml.bind.annotation.XmlSeeAlso;


@XmlRootElement(name = "employee")
@XmlType(name = "", propOrder = {"someContrction"})
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeConstruction extends Employee {

    @XmlElement(required = true)
    protected String someContrction;

    public String getSomeContrction() {
        return someContrction;
    }

    public void setSomeContrction(String someContrction) {
        this.someContrction = someContrction;
    }

}


package com.mycompany.soma.ws.rest.v1.model.test;

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;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "employee")
@XmlType(name = "", propOrder = {"someManaging"})
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeManager extends Employee {

    @XmlElement(required = true)
    protected String someManaging;

    public String getSomeManaging() {
        return someManaging;
    }

    public void setSomeManaging(String someManaging) {
        this.someManaging = someManaging;
    }

}

您可以在newInstance中傳遞幾個類

jaxbContext = JAXBContext.newInstance(Snapshots.class, EmployeeConstruction.class);

您應該將EmployeesElementEmployee類鏈接。 您可以通過兩種方式進行操作:

1)添加@XmlSeeAlso注釋EmplyeesElement

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "employees")
@XmlSeeAlso(Employee.class)
public class EmployeesElement<T> 
{ ...

2)綁定通用類型

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "employees")
public class EmployeesElement<T extends Employee> 
{ ...

這是對我有用的,下面創建了一些類作為示例:

public class Person {}
public class Driver extends Person {}
public class Employee extends Person {}

這通常是層次結構,在創建實例時只需添加類(對於“封送”和“解組”操作):

JAXBContext context = JAXBContext.newInstance(Person.class,Driver.class,Employee.class); //etc

保存文件后,將如下所示:

<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="driver">
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="employee">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM