java 引起:javax.xml.bind.JAXBException:此上下文中已知类或其任何超类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33690508/
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-11-02 22:00:19  来源:igfitidea点击:

Caused by: javax.xml.bind.JAXBException: class nor any of its super class is known to this context

javasoapjaxbmarshalling

提问by Nem

I have the following autogenerated classes from xsd.

我有以下来自 xsd 的自动生成的类。

<xs:element name="Add" type="tns:AddType"/>

<xs:complexType name="AddCatalogDataItem">
    <xs:complexContent>
        <xs:restriction base="tns:AddType">
            <xs:sequence>
                <xs:element ref="tns:Code"/>
                <xs:element ref="tns:Value" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="LoadDataRequest">
    <xs:complexContent>
        <xs:restriction base="AppDataType">
            <xs:sequence>
                <xs:element ref="tns:Code"/>
                <xs:element ref="tns:RegCode" minOccurs="0"/>
                <xs:element ref="tns:Add" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

While creating an request during marshalling stage i'm getting this error:

在编组阶段创建请求时,我收到此错误:

Caused by: javax.xml.bind.JAXBException: class com.ats.vis.services.concentrator.AddCatalogDataItem nor any of its super class is known to this context.

引起:javax.xml.bind.JAXBException:类 com.ats.vis.services.concentrator.AddCatalogDataItem 或其任何超类在此上下文中都是已知的。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LoadDataRequest")
public class LoadDataRequest
    extends AppDataType {}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AddType", propOrder = {
    "any"
})
@XmlSeeAlso({
     AddCatalogDataItem.class,
    AddCatalogDataResult.class
})
public class AddType {}

Here is the piece of code of creation.

这是一段创建代码。

LoadDataRequest appdt = new LoadDataRequest();
AddCatalogDataItem add = new AddCatalogDataItem();              
add.getAny().add(new JAXBElement<String>(new QName(NS_CONC, "Code"),String.class, updateOper.code));
аppdt.getAny().add(add);

And code of marshaller.

和编组员代码。

private SOAPMessage createSOAPRequest(LoadDataRequest request) throws SOAPException, JAXBException {
    SOAPMessage message = MessageFactory.newInstance().createMessage();
    Marshaller marshaller = JAXBContext.newInstance(request.getClass()).createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    JAXBElement<LoadDataRequest> jaxbElement = new JAXBElement<>(new QName(NS_CONC, "LoadDataRequest"), LoadDataRequest.class, request);

    marshaller.marshal(jaxbElement, message.getSOAPBody());

    return message;
}

I'm running out of ideas what is wrong with this code. Please, help.

我已经没有想法了,这段代码有什么问题。请帮忙。

采纳答案by Nem

I've solved the issue by creating jaxb context using following method:

我通过使用以下方法创建 jaxb 上下文解决了这个问题:

JAXBContext jaxb = JAXBContext.newInstance("com.ats.vis.services.concentrator", com.ats.vis.services.concentrator.LoadDataRequest.class.getClassLoader());
    Marshaller marshaller = jaxb.createMarshaller();