JAXB ClassCastException in SOAP handler

Solution Verified - Updated -

Issue

Given the following XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="me">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I want to process the following SOAP header in a handler:

<soapenv:Header>
 <custom:me xmlns:custom="http://ws.examples.jboss.com/">
  <name>Kyle</name>
 </custom:me>
</soapenv:Header>

Like so:

JAXBContext jc = JAXBContext.newInstance("com.jboss.examples.ws"); //custom package name
Unmarshaller u = jc.createUnmarshaller();
String uri = "http://ws.examples.jboss.com/";
SOAPHeader header = msgContext.getMessage().getSOAPHeader();
Node me = header.getElementsByTagNameNS(uri,"me").item(0);
MyObject headerType = (MyObject)u.unmarshal(me);

But I receive the following exception:

java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to com.jboss.examples.ws.MyObject
    at com.jboss.examples.ws.TestHandler.handleMessage(TestHandler.java:56)
    at com.jboss.examples.ws.TestHandler.handleMessage(TestHandler.java:30)
    at org.jboss.ws.core.jaxws.handler.HandlerChainExecutor.handleMessage(HandlerChainExecutor.java:305)
    at org.jboss.ws.core.jaxws.handler.HandlerChainExecutor.handleMessage(HandlerChainExecutor.java:142)
    at org.jboss.ws.core.jaxws.handler.HandlerDelegateJAXWS.callRequestHandlerChain(HandlerDelegateJAXWS.java:97)
    at org.jboss.ws.core.server.ServiceEndpointInvoker.callRequestHandlerChain(ServiceEndpointInvoker.java:129)
    at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(ServiceEndpointInvoker.java:204)
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:470)
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:294)
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:203)
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:129)
    at org.jboss.wsf.common.servlet.AbstractEndpointServlet.service(AbstractEndpointServlet.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Environment

  • JBoss Enterprise Application Platform (EAP) 5.x

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.