Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: JAXB unmarshalling Exception: unexpected element

  1. #11
    Join Date
    Apr 2008
    Posts
    151

    Default

    This is a quirk of JAXB and how it handles resolves using the ObjectFactory versus the XmlRootAnnotation

    http://weblogs.java.net/blog/2006/03...mes-not-always

    The side affect is dealing with marshalled classes directly versus a JAXB wrapped object, e.g. JAXBElement<SomeType>

  2. #12

    Default

    It took me long enough to figure ... but it seems the error persist no matter what I do when using the classesToBeBound property.


    I solved it using two steps on the client side.

    1. Replace classesToBeBound property for contextPath property.
    Code:
    <bean id="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
            <property name="contextPath" value="generated" />
    </bean>
    2. Replace MyObject.class for JAXBElement.class
    Code:
    MyObject obj = (MyObject) restTemplate.getForObject(restUrl, JAXBElement.class, word).getValue();

    Note that both steps where necessary for my success. Finally, I got a contract-first REST service running
    Although, I do think JAXB2 needs to address the @XMLRootElement annotation issue; and I do think spring documentation is failing since javadocs/spring-docs doesn't tell the difference between using classesToBeBound or contextPath when there obviously is a difference.
    Last edited by Toxic; Dec 23rd, 2009 at 12:09 PM.

  3. #13
    Join Date
    May 2011
    Posts
    1

    Default

    I have similar problem on JBoss 5.1 EAP. After sending the request:
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:atl="atlas">
    <soapenv:Header/>
    <soapenv:Body>
    <atl:getAllAccounts>
    <atl:startindex>1</atl:startindex>
    <atl:maxrecords>2</atl:maxrecords>
    </atl:getAllAccounts>
    </soapenv:Body>
    </soapenv:Envelope>

    I got the response:

    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header/>
    <env:Body>
    <env:Fault>
    <faultcode xmlns:valueNS="http://schemas.xmlsoap.org/soap/envelope/">valueNS:Server</faultcode>
    <faultstring xml:lang="en">JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"atlas", local:"getAllAccounts"). Expected elements are (none)</faultstring>
    </env:Fault>
    </env:Body>
    </env:Envelope>

    Here is parts of my WSDL:

    <xsd:complexType name="PaginationRequest">
    <xsd:sequence>
    <xsd:element name="startindex" type="xsd:integer"></xsd:element>
    <xsd:element name="maxrecords" type="xsd:integer"></xsd:element>
    </xsd:sequence>
    </xsd:complexType>



    <xsd:element name="getAllAccounts" type="tns:PaginationRequest" />

    <xsd:element name="getAllAccountsResponse">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="paginationInfo" type="tns:PaginationInfo"></xsd:element>
    <xsd:element name="rwsAccounts" type="tns:ArrayOfRWSAccount"></xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>




    My Java Bean:


    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "getAllAccounts", namespace = "atlas")
    public class GetAllAccountsReq {

    @XmlElement(namespace = "atlas")
    private int startindex;

    @XmlElement(namespace = "atlas")
    private int maxrecords;

    public GetAllAccountsReq(){

    }
    public int getStartindex() {
    return startindex;
    }
    public void setStartindex(int startindex) {
    this.startindex = startindex;
    }

    public int getMaxrecords() {
    return maxrecords;
    }
    public void setMaxrecords(int maxrecords) {
    this.maxrecords = maxrecords;
    }
    }

    Endpoint operation:

    @PayloadRoot(localPart = "getAllAccounts", namespace = NS.TARGET_NS)
    public GetAllAccountsRes getAllAccounts(GetAllAccountsReq req) throws ATLASWSException {
    //Some stub code was here
    return res;
    }


    Spring settings file:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <bean class="org.springframework.ws.transport.http.WsdlD efinitionHandlerAdapter">
    <property name="transformLocations" value="true" />
    </bean>

    <bean class="org.springframework.ws.transport.http.WebSe rviceMessageReceiverHandlerAdapter">
    <property name="messageFactory">
    <bean class="org.springframework.ws.soap.saaj.SaajSoapMe ssageFactory" />
    </property>
    </bean>

    <bean id="atlasWServiceHeaderInterceptor" class="com.custom.ws.listner.Object2SoapHeaderHand ler" />

    <bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapp ing.PayloadRootAnnotationMethodEndpointMapping" >
    <property name="interceptors">
    <list>
    <ref local="atlasWServiceHeaderInterceptor" />
    </list>
    </property>
    </bean>

    <bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.Bea nNameUrlHandlerMapping"/>

    <bean id="atlasWServiceJaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r">
    <property name="classesToBeBound">
    <list>
    <value>com.custom.ws.responses.GetAllAccountsRes </value>
    <value>com.custom.ws.requests.GetAllAccountsReq</value>
    </list>
    </property>
    <property name="marshallerProperties">
    <map>
    <entry key="jaxb.formatted.output">
    <value type="java.lang.Boolean">true</value>
    </entry>
    </map>
    </property>
    </bean>

    <alias name="atlasWServiceJaxbMarshaller" alias="atlasWServiceJaxbUnmarshaller" />

    <bean id="atlasWServiceSoapMessageDispatcher" class="org.springframework.ws.soap.server.SoapMess ageDispatcher">
    <property name="endpointAdapters">
    <list>
    <bean class="org.springframework.ws.server.endpoint.adap ter.MarshallingMethodEndpointAdapter">
    <property name="marshaller" ref="atlasWServiceJaxbMarshaller" />
    <property name="unmarshaller" ref="atlasWServiceJaxbUnmarshaller" />
    </bean>
    </list>
    </property>
    </bean>

    <alias name="atlasWServiceSoapMessageDispatcher" alias="/atlas-webservice"/>

    <bean name="atlasWServiceWsdl" class="org.springframework.ws.wsdl.wsdl11.SimpleWs dl11Definition">
    <property name="wsdl">
    <bean class="org.springframework.core.io.ClassPathResour ce">
    <constructor-arg>
    <value>/ATLASWService.wsdl</value>
    </constructor-arg>
    </bean>
    </property>
    </bean>

    <alias name="atlasWServiceWsdl" alias="/atlasWService.wsdl"/>

    <bean id="atlasWServiceEndpoint" class="com.custom.ws.listner.RPCListnerService"/>

    </beans>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •