创建JAXBContext以将对象编组为XML的问题

时间:2014-11-03 20:46:53

标签: java jaxb

我有一个从XML模式生成的复杂JAXB数据结构。我想将它作为XML编组,仅用于记录事件。对于我遇到的问题,我将非常感谢这个用例的其他解决方案,但即使在这里,也有什么错误:

我找到了以下解决方案:

SomeClass object;
java.io.StringWriter msg = new java.io.StringWriter();
javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext.newInstance(SomeClass.class);
javax.xml.bind.Marshaller marshaller = context.createMarshaller();
// Create a root element since the object is not the root element.
javax.xml.bind.JAXBElement<SomeClass> tolog = new javax.xml.bind.JAXBElement<SomeClass>(
    new javax.xml.namespace.QName("SomeClass"), SomeClass.class, object);
marshaller.marshal(tolog, msg);
// Output msg somewhere, like sysout:
System.out.println(msg.toString());

我尝试过这两种方式,每种方式都会导致错误。

  1. 我在静态上下文中创建了JAXBContext对象,以避免每次都重新创建它。这似乎在某种程度上混淆了类注册表,因为只有当我在静态时间调用JAXBContext.newInstance(SomeClass.class)时,我才尝试实例化自定义类时获得java.lang.NoClassDefFoundError
  2. 我在第一次需要时创建了JAXBContext对象(这是在将JAXB对象作为参数的EJB方法中),在第一次之后将其存储到静态变量中。这一次,对JAXBContext.newInstance(SomeClass.class)的调用引发了以下异常:

    java.lang.ClassCastException: mypackage._1.NameType$JaxbAccessorF_value cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor
      at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.instanciate(OptimizedAccessorFactory.java:199)
      at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:191)
      at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:282)
      at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.<init>(TransducedAccessor.java:237)
      at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:177)
      at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.calcTransducer(RuntimeClassInfoImpl.java:245)
      at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.getTransducer(RuntimeClassInfoImpl.java:219)
      at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.link(RuntimeClassInfoImpl.java:196)
      at com.sun.xml.bind.v2.model.impl.ModelBuilder.link(ModelBuilder.java:399)
      at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.link(RuntimeModelBuilder.java:138)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:464)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:298)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:141)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1163)
      at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:145)
      at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:236)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:172)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:132)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:334)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:431)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:394)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:298)
      at mypackage.MainBean.SOAPCall(MainBean.java:108)
    
  3. 我收到了依赖树的请求。这是:

    [INFO] mypackage:Main:war:0.0.1-SNAPSHOT
    [INFO] +- mypackage:datatypes:jar:1.0.5-SNAPSHOT:compile
    [INFO] +- org.apache.commons:commons-lang3:jar:3.3.2:compile
    [INFO] +- org.slf4j:slf4j-jdk14:jar:1.7.6:compile
    [INFO] |  \- org.slf4j:slf4j-api:jar:1.7.6:compile
    [INFO] +- org.apache.openejb:javaee-api:jar:6.0-5:provided
    [INFO] +- junit:junit:jar:4.11:test
    [INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
    [INFO] +- org.apache.openejb:tomee-embedded:jar:1.0.0:test
    [INFO] |  +- org.apache.openejb:openejb-jsf:jar:4.0.0:test
    [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-impl:jar:1.1.4:test
    [INFO] |  |  |  \- net.sf.scannotation:scannotation:jar:1.0.2:test
    [INFO] |  |  +- org.apache.myfaces.core:myfaces-api:jar:2.1.7:test
    [INFO] |  |  \- org.apache.openejb.patch:openejb-openwebbeans-jsf:jar:1.1.4:test
    [INFO] |  +- org.apache.openejb:openejb-core:jar:4.0.0:test
    [INFO] |  |  +- org.apache.openejb:mbean-annotation-api:jar:4.0.0:test
    [INFO] |  |  +- org.apache.openejb:openejb-api:jar:4.0.0:test
    [INFO] |  |  +- org.apache.openejb:openejb-loader:jar:4.0.0:test
    [INFO] |  |  +- org.apache.openejb:openejb-javaagent:jar:4.0.0:test
    [INFO] |  |  +- org.apache.openejb:openejb-jee:jar:4.0.0:test
    [INFO] |  |  |  \- com.sun.xml.bind:jaxb-impl:jar:2.2.5:test
    [INFO] |  |  +- commons-cli:commons-cli:jar:1.2:test
    [INFO] |  |  +- org.apache.activemq:kahadb:jar:5.5.1:test
    [INFO] |  |  +- org.apache.activemq:activemq-ra:jar:5.5.1:test
    [INFO] |  |  +- org.apache.activemq:activemq-core:jar:5.5.1:test
    [INFO] |  |  |  +- org.apache.geronimo.specs:geronimo-jms_1.1_spec:jar:1.1.1:test
    [INFO] |  |  |  +- org.apache.activemq:activeio-core:jar:3.1.2:test
    [INFO] |  |  |  +- org.apache.activemq.protobuf:activemq-protobuf:jar:1.1:test
    [INFO] |  |  |  +- org.apache.geronimo.specs:geronimo-j2ee-management_1.1_spec:jar:1.0.1:test
    [INFO] |  |  |  \- commons-net:commons-net:jar:2.0:test
    [INFO] |  |  +- org.apache.geronimo.components:geronimo-connector:jar:3.1.1:test
    [INFO] |  |  |  \- org.apache.geronimo.specs:geronimo-j2ee-connector_1.6_spec:jar:1.0:test
    [INFO] |  |  +- org.apache.geronimo.components:geronimo-transaction:jar:3.1.1:test
    [INFO] |  |  +- org.objectweb.howl:howl:jar:1.0.1-1:test
    [INFO] |  |  +- org.apache.geronimo.javamail:geronimo-javamail_1.4_mail:jar:1.8.2:test
    [INFO] |  |  +- org.apache.xbean:xbean-asm-shaded:jar:3.10:test
    [INFO] |  |  +- org.apache.openejb.patch:xbean-finder-shaded:jar:3.10:test
    [INFO] |  |  +- org.apache.xbean:xbean-reflect:jar:3.10:test
    [INFO] |  |  +- org.apache.xbean:xbean-naming:jar:3.10:test
    [INFO] |  |  +- org.apache.xbean:xbean-bundleutils:jar:3.10:test
    [INFO] |  |  +- org.hsqldb:hsqldb:jar:2.2.4:test
    [INFO] |  |  +- commons-dbcp:commons-dbcp:jar:1.4:test
    [INFO] |  |  +- commons-pool:commons-pool:jar:1.5.7:test
    [INFO] |  |  +- org.codehaus.swizzle:swizzle-stream:jar:1.6.1:test
    [INFO] |  |  +- wsdl4j:wsdl4j:jar:1.6.2:test
    [INFO] |  |  +- org.quartz-scheduler:quartz:jar:2.1.3:test
    [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-spi:jar:1.1.4:test
    [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-ejb:jar:1.1.4:test
    [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-ee:jar:1.1.4:test
    [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-ee-common:jar:1.1.4:test
    [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-web:jar:1.1.4:test
    [INFO] |  |  +- org.javassist:javassist:jar:3.15.0-GA:test
    [INFO] |  |  +- org.apache.openejb.patch:openjpa-asm-shaded:jar:2.2.0:test
    [INFO] |  |  |  \- net.sourceforge.serp:serp:jar:1.13.1:test
    [INFO] |  |  +- org.apache.bval:bval-core:jar:0.4:test
    [INFO] |  |  |  \- commons-beanutils:commons-beanutils-core:jar:1.8.3:test
    [INFO] |  |  \- org.apache.bval:bval-jsr303:jar:0.4:test
    [INFO] |  +- org.apache.myfaces.core:myfaces-impl:jar:2.1.7:test
    [INFO] |  |  +- commons-collections:commons-collections:jar:3.2:test
    [INFO] |  |  +- commons-beanutils:commons-beanutils:jar:1.8.3:test
    [INFO] |  |  |  \- commons-logging:commons-logging:jar:1.1.1:test
    [INFO] |  |  \- commons-digester:commons-digester:jar:1.8:test
    [INFO] |  +- javax.servlet:jstl:jar:1.2:test
    [INFO] |  +- org.apache.tomcat:tomcat-catalina:jar:7.0.27:test
    [INFO] |  |  +- org.apache.tomcat:tomcat-servlet-api:jar:7.0.27:test
    [INFO] |  |  +- org.apache.tomcat:tomcat-juli:jar:7.0.27:test
    [INFO] |  |  +- org.apache.tomcat:tomcat-annotations-api:jar:7.0.27:test
    [INFO] |  |  +- org.apache.tomcat:tomcat-api:jar:7.0.27:test
    [INFO] |  |  \- org.apache.tomcat:tomcat-util:jar:7.0.27:test
    [INFO] |  +- org.apache.tomcat:tomcat-catalina-ha:jar:7.0.27:test
    [INFO] |  |  \- org.apache.tomcat:tomcat-tribes:jar:7.0.27:test
    [INFO] |  +- org.apache.tomcat:tomcat-coyote:jar:7.0.27:test
    [INFO] |  +- org.apache.tomcat:tomcat-jasper:jar:7.0.27:test
    [INFO] |  |  +- org.apache.tomcat:tomcat-jsp-api:jar:7.0.27:test
    [INFO] |  |  +- org.apache.tomcat:tomcat-el-api:jar:7.0.27:test
    [INFO] |  |  \- org.apache.tomcat:tomcat-jasper-el:jar:7.0.27:test
    [INFO] |  +- org.eclipse.jdt.core.compiler:ecj:jar:3.5.1:test
    [INFO] |  +- org.apache.velocity:velocity:jar:1.6.4:test
    [INFO] |  |  +- commons-lang:commons-lang:jar:2.4:test
    [INFO] |  |  \- oro:oro:jar:2.0.8:test
    [INFO] |  +- org.apache.geronimo.specs:geronimo-j2ee-deployment_1.1_spec:jar:1.1:test
    [INFO] |  +- org.apache.openejb:openejb-client:jar:4.0.0:test
    [INFO] |  \- org.apache.openejb:tomee-catalina:jar:4.0.0:test
    [INFO] |     +- org.apache.openejb:javaee-api:jar:tomcat:6.0-3:test
    [INFO] |     +- org.apache.openejb:tomee-common:jar:4.0.0:test
    [INFO] |     +- org.apache.openejb:tomee-loader:jar:4.0.0:test
    [INFO] |     |  \- com.google.code.gson:gson:jar:2.1:test
    [INFO] |     +- org.apache.openejb:openejb-server:jar:4.0.0:test
    [INFO] |     +- org.apache.openejb:openejb-ejbd:jar:4.0.0:test
    [INFO] |     \- org.apache.openejb:openejb-http:jar:4.0.0:test
    [INFO] \- commons-codec:commons-codec:jar:1.3:compile
    

    出了什么问题,我该怎么做呢?

0 个答案:

没有答案