此上下文中已知javax.xml.bind.MarshalException及其任何超类

时间:2014-05-07 06:17:12

标签: java xml web-services jaxb marshalling

我超越了异常,寻找解决方案,任何帮助将不胜感激。在其他一些消息中发现了同样的问题,但它们并不适用于我。请参阅以下代码。

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

}

3 个答案:

答案 0 :(得分:3)

您可以在newInstance中传递几个类

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

答案 1 :(得分:1)

您应该将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> 
{ ...

答案 2 :(得分:0)

这里有什么对我有用,下面创建的类作为例子:

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

这通常是层次结构,在创建实例时只需添加类(对于&#34; marshal&#34;和&#34; unmarshal&#34;操作):

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">