JAXB
生成された 1 つのオブジェクトをにマーシャリングする必要がありますString
。@XmlRootElement
問題は、注釈がないことです。ルート要素ではありません。の要素はXSD
次のようになります。
<xs:complexType name="CompletedAssessmentInstance">
<xs:complexContent mixed="false">
<xs:extension base="tns:AssessmentInstance">
<xs:sequence>
<xs:element minOccurs="0" name="CompletionDate" type="xs:dateTime"/>
<xs:element minOccurs="0" name="CustomResultsXML" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="NormedScores" nillable="true" type="tns:NormedScores"/>
<xs:element minOccurs="0" name="RawScore" type="xs:decimal"/>
<xs:element minOccurs="0" name="TimeTaken" type="xs:int"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="CompletedAssessmentInstance" nillable="true" type="tns:CompletedAssessmentInstance"/>
tns
プレフィックスはこの名前空間を指します: xmlns:tns="http://www.cubiksonline.com/2009/08/AssessmentProvider"
XML
ルート要素ではないマーシャリングを行うためにこれを行うことができることがわかりましたJAXB
:
public static String completedAssessmentInstanceToString(CompletedAssessmentInstance assessmentInstance)
throws AssessmentException {
try {
Marshaller marshaller = jc.createMarshaller();
StringWriter writer = new StringWriter();
QName qname = new QName(
"http://www.cubiksonline.com/2009/08/AssessmentProvider",
"CompletedAssessmentInstance");
JAXBElement<CompletedAssessmentInstance> rootElement = new JAXBElement<CompletedAssessmentInstance>(
qname,
CompletedAssessmentInstance.class,
assessmentInstance);
marshaller.marshal(rootElement, writer);
return writer.toString();
} catch (JAXBException ex) {
LOGGER.error("JAXB Exception occurred.", ex);
throw new AssessmentException(RequestStatusValue.InvalidProjectConfiguration,
"Unable to marshal the CompletedAssessmentInstance data: " + assessmentInstance, ex);
}
}
JAXB
次のようにコンテキストを初期化しています。
private static JAXBContext jc = createJaxbContext();
private static JAXBContext createJaxbContext() {
try {
ClassLoader cl = com.cubiksonline._2009._08.assessmentprovider.ObjectFactory.class.getClassLoader();
return JAXBContext.newInstance("com.cubiksonline._2009._08.assessmentprovider", cl);
} catch (JAXBException ex) {
LOGGER.error("Failed to create JAXB context: ", ex);
return null;
}
}
このパッケージ:com.cubiksonline._2009._08.assessmentprovider
は、 および を含むすべてのJAXB
生成されたクラスがあるパッケージCompletedAssessmentInstance
です。問題は、marshaller.marshal(rootElement, writer);
が呼び出されたときに次の例外が発生することです。
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: com.cubiksonline._2009._08.assessmentprovider.CompletedAssessmentInstance is not known to this context]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:318)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95)
at com.groupgti.esb.assessments.cubiks.CubiksFactory.completedAssessmentInstanceToString(CubiksFactory.java:272)[647:com.groupgti.esb.online.tests.cubiks:1.2.0.SNAPSHOT]
... 40 more
Caused by: javax.xml.bind.JAXBException: com.cubiksonline._2009._08.assessmentprovider.CompletedAssessmentInstance is not known to this context
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:246)
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:261)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:144)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:189)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:316)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:323)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:72)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315)
... 43 more
Caused by: javax.xml.bind.JAXBException: com.cubiksonline._2009._08.assessmentprovider.CompletedAssessmentInstance is not known to this context
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:625)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:141)
... 49 more
誰がどこに問題があるのか 知っていますか?