This discussion is archived
0 Replies Latest reply: May 25, 2011 3:16 AM by Arturo RSS

javax.xml.bind.JAXBElement cannot be cast

Arturo Newbie
Currently Being Moderated
Hi
I have an XSD like this:

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://poc.cmc.com/ScreenLayout"
xmlns:tns="http://poc.cmc.com/ScreenLayout" elementFormDefault="qualified">
<complexType name="Action">
<sequence>
<element name="Display" type="tns:Screen"/>
<element name="ActionDetail" type="tns:DefLinks"/>
</sequence>
</complexType>
...
If I generate the JAXB classes and I create an XML I can marshall and unmarshall the XML without problem. The XML file looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<tns:Action xmlns:tns="http://poc.cmc.com/ScreenLayout"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://poc.cmc.com/ScreenLayout Screen.xsd ">
<tns:Display>
<tns:ScreenSections>
..
</tns:Action>

Now, I created a WSDL (which imports that XSD) and a service implementation :
<definitions

targetNamespace="urn:BRPOC"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:ns1="http://poc.cmc.com/ScreenLayout"
xmlns:tns="urn:BRPOC"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
>
<types>
<xsd:schema targetNamespace="urn:BRPOC" elementFormDefault="qualified">
<xsd:import namespace="http://poc.cmc.com/ScreenLayout" schemaLocation="Screen.xsd"/>
<xsd:element name="request" type="ns1:Action"/>
<xsd:element name="response" type="ns1:Action"/>
<xsd:element name="Fault" type="ns1:Fault"/>
</xsd:schema>
</types>
. . .

Now, if I try to use the JAXB classes generated during the service implementation I get an error like this:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://poc.cmc.com/ScreenLayout", local:"Action"). Expected elements are <{urn:BRPOC}Fault>,<{urn:BRPOC}request>,<{urn:BRPOC}response>

I created an XML file with request element:

<?xml version="1.0" encoding="UTF-8"?>

<tns1:request xmlns:tns1="urn:BRPOC"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="Screen.xsd">
          <tns:Action>
. . .

But now I get: java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to com.xxx.yyy.ws.client.types.Action

How can I re-use the JAXB classes generated during the creation of a eeb service from a WSDL?

Thanks a lot

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points