当前位置: 首页 > 知识库问答 >
问题:

编组错误:javax。xml。绑定JAXBException:类及其任何超类都不为本文所知

东郭存
2023-03-14

当我在编组一个对象时,我得到了这个异常:javax.xml.bind.JAXBExctive: class订阅,它的任何超类对此上下文都是已知的。

我知道有@xmlsee和修改jaxb类的解决方案,但当我们从XSD/WSDL文件生成jaxb类时,我们无法更改它们。因此,这些解决方案不适用于这种情况。

  public static String getStringFromSubscription(Subscription subscription) throws MbException
  {
Marshaller marshaller;
StringWriter stringWriter = new StringWriter();
    try
    {
      marshaller = JAXBContext.newInstance(com.myhealth.com.ObjectFactory.class
            .getPackage().getName()).createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
      marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
marshaller.marshal(subscription, stringWriter);
    }
    catch (Exception e)
    {
      throw new MbException(e);
    }
    return stringWriter;
}

共有2个答案

笪欣嘉
2023-03-14

据我所知,有三种解决方案

ObjectFactory类是在从xsd/wsdl创建jaxb时自动创建的。

  1. 使用ObjectFactory方法创建必要的对象
marshaller.marshal(new com.myhealth.com.ObjectFactory().createSubscription(subscription), stringWriter);
JAXBContext.newInstance(Subscription.class).createMarshaller();
JAXBContext.newInstance(com.myhealth.com.ObjectFactory.class
            .getPackage().getName()).createMarshaller();
孟和玉
2023-03-14

您需要在JAXBContext.new实例化中指定上下文(也称为包名)名称。它将定位位于该包中的ObjectFactory.class,如留档(pt.1)中所示。

如果在创建JAXBContext时遇到错误,例如

  1. 无法在包中找到ObjectFactory.class或jaxb.index
  2. conextPath中包含的全局元素之间的歧义
  3. 未能找到上下文工厂提供程序属性的值
  4. 在同一ConextPath上混合来自不同提供程序的架构派生包
public static String getStringFromSubscription(Subscription subscription) throws MbException {
    Marshaller marshaller;
    StringWriter stringWriter = new StringWriter();
    try {
        marshaller = JAXBContext.newInstance("com.myhealth.com").createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
        marshaller.marshal(subscription, stringWriter);
    } catch (Exception e) {
        throw new MbException(e);
    }
    return stringWriter;
}

 类似资料: