javax.xml.bind.MarshalException 或其任何超类在此上下文中都是已知的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23510132/
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
javax.xml.bind.MarshalException nor any of its super class is known to this context
提问by user3157090
I am getting above exception, looking for solution, any help will be much appreciated. Found the same issue in some other messages, but they were not working for me. Please see the below code.
我正在超越异常,正在寻找解决方案,任何帮助将不胜感激。在其他一些消息中发现了同样的问题,但它们对我不起作用。请看下面的代码。
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;
}
}
回答by Ilya
You should link EmployeesElement
with Employee
class.
You can do it in two ways:
你应该EmployeesElement
和Employee
班级联系起来。您可以通过两种方式做到这一点:
1) Add @XmlSeeAlso
annotation to EmplyeesElement
1) 添加@XmlSeeAlso
注释EmplyeesElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "employees")
@XmlSeeAlso(Employee.class)
public class EmployeesElement<T>
{ ...
2) Bound generic type
2) 绑定泛型类型
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "employees")
public class EmployeesElement<T extends Employee>
{ ...
回答by OLemonOfLife
You can pass several classes in newInstance
你可以在 newInstance 中传递几个类
jaxbContext = JAXBContext.newInstance(Snapshots.class, EmployeeConstruction.class);
回答by Veesta
Here's what worked for me, classes created below as example:
这是对我有用的方法,下面创建的类作为示例:
public class Person {}
public class Driver extends Person {}
public class Employee extends Person {}
That is generally the hierarchy, when creating an instance just add the classes (for both "marshal" and "unmarshal" operations):
这通常是层次结构,在创建实例时只需添加类(对于“编组”和“解组”操作):
JAXBContext context = JAXBContext.newInstance(Person.class,Driver.class,Employee.class); //etc
Once you save your file it will be something like this:
保存文件后,它将是这样的:
<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">