Announcement Announcement Module
Collapse
No announcement yet.
JAXB unmarshalling Exception: unexpected element Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JAXB unmarshalling Exception: unexpected element

    Hello,

    I'm having problems while trying to use JAXB2 marshaller.

    When I send a request to my spring WS endpoint, I get the following

    Code:
    org.springframework.oxm.jaxb.JaxbUnmarshallingFailureException: JAXB unmarshalling exception: unexpected element (uri:"http://prov.m2.testing.es/", local:"CreateCustomerRequest"). Expected elements are (none); nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"http://prov.m2.testing.es/", local:"CreateCustomerRequest"). Expected elements are (none)
    Caused by:
    javax.xml.bind.UnmarshalException: unexpected element (uri:"http://prov.m2.testing.es/", local:"CreateCustomerRequest"). Expected elements are (none)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContex
    t.handleEvent(UnmarshallingContext.java:523)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(
    Loader.java:199)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(
    Loader.java:194)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpe
    ctedChildElement(Loader.java:71)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContex
    t$DefaultRootLoader.childElement(UnmarshallingContext.java:920)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContex
    t._startElement(UnmarshallingContext.java:366)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContex
    t.startElement(UnmarshallingContext.java:347)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshal
    ler.startElement(ValidatingUnmarshaller.java:74)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor
    .startElement(InterningXmlVisitor.java:35)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startE
    lement(SAXConnector.java:101)
            at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.ja
    va:224)
            at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.jav
    a:107)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.un
    marshal0(UnmarshallerImpl.java:288)
            at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.un
    marshal(UnmarshallerImpl.java:271)
            at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnm
    arshallerImpl.java:107)
            at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshalle
    r.java:296)
            at org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndp
    oint.unmarshalRequest(AbstractMarshallingPayloadEndpoint.java:146)
            at org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndp
    oint.invoke(AbstractMarshallingPayloadEndpoint.java:133)
            at org.springframework.ws.server.endpoint.adapter.MessageEndpointAdapter
    .invoke(MessageEndpointAdapter.java:36)
            at org.springframework.ws.server.MessageDispatcher.dispatch(MessageDispa
    tcher.java:198)
            at org.springframework.ws.server.MessageDispatcher.receive(MessageDispat
    cher.java:145)
    Here is my configured bean for the marshaller:
    Code:
        <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>es.testing.m2.prov.model.ActionType</value>
                    <value>es.testing.m2.prov.model.CreateSubscriberServiceRequest</value>
                    <value>es.testing.m2.prov.model.CreateSubscriberServiceRequest</value>
                    <value>es.testing.m2.prov.model.ModifyCustomerStatusRequest</value>
                    <value>es.testing.m2.prov.model.ModifySubscriberServiceParametersRequest</value>
                    <value>es.testing.m2.prov.model.ModifySubscriberServiceStatusRequest</value>
                    <value>es.testing.m2.prov.model.SubscriberServiceInfoRequest</value>
                    <value>es.testing.m2.prov.model.SubscriberServiceInfoResponse</value>
                    <value>es.testing.m2.prov.model.CreateCustomerRequest</value>
                </list>
            </property>
            <property name="schema" value="/WEB-INF/Prov.xsd"/>
        </bean>
    And here is my schema
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://prov.m2.testing.es/" xmlns:prov="http://prov.m2.testing.es/" elementFormDefault="qualified">
    
        <complexType name="CreateSubscriberServiceRequest">
        	<sequence>
        		<element name="WorkOrder" type="string"></element>
        		<element name="Subscriber" type="string"></element>
        		<element name="Service" type="string"></element>
        		<element name="ServiceClass" type="string"></element>
        		<element name="Customer" type="string"></element>
        	</sequence>
        </complexType>
        <complexType name="CreateCustomerRequest">
        	<sequence>
        		<element name="CustomerId" type="string"></element>
        		<element name="CustomerName" type="string"></element>
        	</sequence>
        </complexType>
        <complexType name="ModifySubscriberServiceParametersRequest">
        	<sequence>
        		<element name="WorkOrder" type="string"></element>
        		<element name="Subscriber" type="string"></element>
        		<element name="Service" type="string"></element>
    	   		<element name="ServiceClass" type="string"></element>
        		<element name="Customer" type="string"></element>
        	</sequence>
        </complexType>
        
        <complexType name="ModifySubscriberServiceStatusRequest">
        	<sequence>
        		<element name="WorkOrder" type="string"></element>
        		<element name="Subscriber" type="string"></element>
        		<element name="Service" type="string"></element>
        		<element name="Action" type="prov:ActionType"></element>
        	</sequence>
        </complexType>
        
    
        <simpleType name="ActionType">
        	<restriction base="string">
        		<enumeration value="Enable"></enumeration>
        		<enumeration value="Disable"></enumeration>
        		<enumeration value="Block"></enumeration>
        		<enumeration value="Unblock"></enumeration>
        	</restriction>
        </simpleType>
    
        <complexType name="ModifyCustomerStatusRequest">
        	<sequence>
        		<element name="WorkOrder" type="string"></element>
        		<element name="Customer" type="string"></element>
        		<element name="Action" type="prov:ActionType"></element>
        	</sequence>
        </complexType>
    
        <complexType name="SubscriberServiceInfoResponse">
        	<sequence>
        		<element name="Id" type="int"></element>
        		<element name="Subscriber" type="string"></element>
        		<element name="Service" type="string"></element>
        		<element name="ServiceClass" type="string"></element>
        		<element name="Customer" type="string"></element>
        		<element name="Status" type="string"></element>
        	</sequence>
        </complexType>
    
        <complexType name="SubscriberServiceInfoRequest">
        	<sequence>
        		<element name="Subscriber" type="string"></element>
        		<element name="Service" type="string"></element>
        	</sequence>
        </complexType>
    
        <element name="CreateCustomerRequest" type="prov:CreateCustomerRequest"></element>
        <element name="CreateSubscriberServiceRequest" type="prov:CreateSubscriberServiceRequest"></element>
        <element name="ModifyCustomerStatusRequest" type="prov:ModifyCustomerStatusRequest"></element>
        <element name="ModifySubscriberServiceParametersRequest" type="prov:ModifySubscriberServiceParametersRequest"></element>
        <element name="ModifySubscriberServiceStatusRequest" type="prov:ModifySubscriberServiceStatusRequest"></element>
        <element name="SubscriberServiceInfoRequest" type="prov:SubscriberServiceInfoRequest"></element>
        <element name="SubscriberServiceInfoResponse" type="prov:SubscriberServiceInfoResponse"></element>
    </schema>

  • #2
    That's weird, everything looks good as far as I can see. Can you send any of the other *Requests, or is it just the CreateCustomerRequest()?

    Comment


    • #3
      I've tested all of them.

      I've added @XmlRootElement annotation to all my Request/Response XSD elements, and now I have the following:
      Code:
      DEBUG 2007-06-17 13:52:05,224 http-8080-Processor25 org.springframework.ws.soap.server.SoapMessageDispatcher: MessageDispatcher with name 'spring-ws'
      received request [SaajSoapMessage {http://prov.m2.testing.es/}CreateCustomerRequest]
      DEBUG 2007-06-17 13:52:05,271 http-8080-Processor25 org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping: Looking up endpoin
      t for [{http://prov.m2.testing.es/}CreateCustomerRequest]
      DEBUG 2007-06-17 13:52:05,271 http-8080-Processor25 org.springframework.ws.soap.server.SoapMessageDispatcher: Endpoint mapping [org.springframework.ws
      .server.endpoint.mapping.PayloadRootQNameEndpointMapping@1896a4c] maps request to endpoint [es.testing.m2.prov.web.CustomerEndpoint@b6ef8]
      DEBUG 2007-06-17 13:52:05,271 http-8080-Processor25 org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor: Request: <CreateCust
      omerRequest xmlns="http://prov.m2.testing.es/"><CustomerId>123</CustomerId><CustomerName>Pepito P┌rez</CustomerName></CreateCustomerRequest>
      DEBUG 2007-06-17 13:52:05,287 http-8080-Processor25 org.springframework.ws.soap.server.SoapMessageDispatcher: Testing endpoint adapter [org.springfram
      ework.ws.server.endpoint.adapter.MessageEndpointAdapter@1bf68a9]
      DEBUG 2007-06-17 13:52:05,287 http-8080-Processor25 org.springframework.ws.soap.server.SoapMessageDispatcher: Testing endpoint exception resolver [org
      .springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver@688954]
      WARN  2007-06-17 13:52:05,302 http-8080-Processor25 org.springframework.ws.soap.server.SoapMessageDispatcher: Endpoint invocation resulted in exceptio
      n - responding with SOAP Fault
      org.springframework.oxm.jaxb.JaxbUnmarshallingFailureException: JAXB unmarshalling exception: unexpected element (uri:"http://prov.m2.testing.es/", l
      ocal:"CreateCustomerRequest"). Expected elements are <{http://prov.m2.testing.es/}createCustomerRequest>,<{http://prov.m2.testing.es/}createSubscrib
      erServiceRequest>,<{http://prov.m2.testing.es/}modifyCustomerStatusRequest>,<{http://prov.m2.testing.es/}modifySubscriberServiceParametersRequest>,<
      {http://prov.m2.testing.es/}modifySubscriberServiceStatusRequest>,<{http://prov.m2.testing.es/}subscriberServiceInfoRequest>,<{http://prov.m2.metror
      ed.es/}subscriberServiceInfoResponse>; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"http://prov.m2.testing.es/", l
      ocal:"CreateCustomerRequest"). Expected elements are <{http://prov.m2.testing.es/}createCustomerRequest>,<{http://prov.m2.testing.es/}createSubscrib
      erServiceRequest>,<{http://prov.m2.testing.es/}modifyCustomerStatusRequest>,<{http://prov.m2.testing.es/}modifySubscriberServiceParametersRequest>,<
      {http://prov.m2.testing.es/}modifySubscriberServiceStatusRequest>,<{http://prov.m2.testing.es/}subscriberServiceInfoRequest>,<{http://prov.m2.testing
      .es/}subscriberServiceInfoResponse>
      Caused by:
      javax.xml.bind.UnmarshalException: unexpected element (uri:"http://prov.m2.testing.es/", local:"CreateCustomerRequest"). Expected elements are <{http
      ://prov.m2.testing.es/}createCustomerRequest>,<{http://prov.m2.testing.es/}createSubscriberServiceRequest>,<{http://prov.m2.testing.es/}modifyCusto
      merStatusRequest>,<{http://prov.m2.testing.es/}modifySubscriberServiceParametersRequest>,<{http://prov.m2.testing.es/}modifySubscriberServiceStatusR
      equest>,<{http://prov.m2.testing.es/}subscriberServiceInfoRequest>,<{http://prov.m2.testing.es/}subscriberServiceInfoResponse>
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:523)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:199)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:194)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:71)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:920)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:366)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:347)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:74)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:35)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
              at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:224)
              at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:107)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:288)
              at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:271)
              at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:107)
              at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:296)
              at org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint.unmarshalRequest(AbstractMarshallingPayloadEndpoint.java:146)
              at org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint.invoke(AbstractMarshallingPayloadEndpoint.java:133)
              at org.springframework.ws.server.endpoint.adapter.MessageEndpointAdapter.invoke(MessageEndpointAdapter.java:36)
              at org.springframework.ws.server.MessageDispatcher.dispatch(MessageDispatcher.java:198)
              at org.springframework.ws.server.MessageDispatcher.receive(MessageDispatcher.java:145)
              at org.springframework.ws.transport.support.WebServiceMessageReceiverObjectSupport.handleConnection(WebServiceMessageReceiverObjectSupport.jav
      a:84)
              at org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter.handle(WebServiceMessageReceiverHandlerAdapter.java:56)
              at org.springframework.ws.transport.http.MessageDispatcherServlet.doService(MessageDispatcherServlet.java:123)
              at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:461)
              at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:426)
              at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
              at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
              at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
              at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
              at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
              at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
              at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
              at java.lang.Thread.run(Thread.java:619)
      DEBUG 2007-06-17 13:52:05,334 http-8080-Processor25 org.springframework.ws.soap.server.SoapMessageDispatcher: MessageDispatcher with name 'spring-ws'
      sends response [SaajSoapMessage {http://schemas.xmlsoap.org/soap/envelope/}Fault] for request [SaajSoapMessage {http://prov.m2.testing.es/}CreateCust
      omerRequest]
      DEBUG 2007-06-17 13:52:05,349 http-8080-Processor25 org.springframework.ws.transport.http.MessageDispatcherServlet: Successfully completed request
      DEBUG 2007-06-17 13:52:05,349 http-8080-Processor25 org.springframework.web.context.support.XmlWebApplicationContext: Publishing event in context [org
      .[email protected]28]: ServletRequestHandledEvent: url=[/es.testing.m2.prov/ws/networkProvision/]; cl
      ient=[127.0.0.1]; method=[POST]; servlet=[spring-ws]; session=[null]; user=[null]; time=[187ms]; status=[OK]
      Notice that JAXB expets all the messages starting in lower case! that's strange, I've defined them as upper-case in my schema
      Last edited by flozano; Jun 17th, 2007, 08:05 AM.

      Comment


      • #4
        Did you create the objects from the schema using xjc, or did you create them by hand? I recommend using xjc, since that makes sure that the types are the way JAXB expects them.

        The XJC compiler also generates a file called ObjectFactory, which shows you how to create elements. It might have something like a createCustomerRequest() method, which returns a JAXBElement<CustomerRequest> or something similar. The way to make it work is to use those types, rather than the raw CreateCustomerRequest. Look in the RC2 airline sample to see how I made JAXB2 work.

        Comment


        • #5
          Yes, I created all the objects using XJC.

          I can't use ObjectFactory because my client isn't a java one but a .NET one... but that shouldn't matter, as if you take a look at the log, you'll see the XML payload created by the remote .NET side is conform to the XSD.

          Anyway, I've just solved it!!!!!

          I modified all the XmlRootElement and added a name="ComplexTypeName"... and now it works!

          I guess it's a JAXB2 bug...

          Comment


          • #6
            Hi,

            I am getting the same exception with Jaxb2Marshaller as you had as below:

            [com.sun.istack.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation]
            at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(M arshallerImpl.java:295)
            I have created the DTO classes using xjc and it has not added @XmlRootElement annotation.
            Can you please tell me how you had fixed this issue?

            Thanks,
            Sulabha

            Comment


            • #7
              Rather than use @XmlRootElement annotations, you probably have an ObjectFactory class generated for you. This class contains factory methods of the form

              Code:
              public JAXBElement<X> createX(X something)
              The trick is to use these JAXBElement types in your method signatures, just as I did in the Spring-WS Airline example.

              Comment


              • #8
                Thanks Arjen.

                It was my mistake. It created DTOs with annotation. However my service class was retruning String instead of DTO, hence it was giving missing annotation error for String.

                Comment


                • #9
                  Hi Arjen,

                  This is not related to this thread..I have a query about designing endpoints in
                  spring web service.

                  Typically there should be only endpoint in spring w/s that will marshall/unmarshall & route the call to business class?


                  Lets say..if I'm implementing AbstractMarshallingPayloadEndpoint, I will have to check the type of object passed as the input parameter in invokeInternal() method and accordingly I can call the business methods like getFlights() or bookFlights().

                  Is there any other better design approach? Can I put an interface like facade for the clients?
                  If there is already any thread/article having discussion about this, can you please send me that?

                  Many Thanks,
                  Sulabha

                  Comment


                  • #10
                    I'll extends this thread with my own problem, as it relates exactly to that of the first post: An unmarshalling exception for unexpected element.


                    Invoking the client code:
                    Code:
                            restTemplate.getForObject("url", myObject.class, "params");
                    Gives the following exception:
                    Code:
                        javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"myObject"). Expected elements are (none)


                    Server side
                    * JAXB2 to marshall objects into XML
                    * I'm using xjc to generate an XSD from java beans on the server side.
                    * Each exposed object is annoted with @XMLRootElement.


                    Client side
                    * JAXB2 to demarshall XML into objects
                    * I'm using xjc to generate java objects from XSD on the client side.
                    * The demarshalling works if I copy the source files of my objects from the server side, and use JAXB2 as a demarshaller.
                    * The demarshalling does not work if I use xjc to generate client objects from XML before letting JAXB2 populate them


                    Client side configuration


                    applicationContext-client.xml
                    Code:
                        <!-- My REST client injected with spring RestTemplate -->
                        <bean id="restClient" class="test.RestClient">
                            <property name="restTemplate" ref="restTemplate"/>
                        </bean>
                    
                    
                        <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
                            <property name="messageConverters">
                                <list>
                                    <bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                                        <property name="marshaller" ref="xmlMarshaller" />
                                        <property name="unmarshaller" ref="xmlMarshaller" />
                                    </bean>
                                </list>
                            </property>
                        </bean>
                    
                    
                       <bean id="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
                            <property name="classesToBeBound">
                                <list>
                                    <value>generated.myObject</value>
                                </list>
                            </property>
                            <property name="schema" value="schema.xsd" />
                        </bean>

                    My restClient
                    Code:
                    public class restClient {
                        RestTemplate restTemplate;
                    
                        public void doStuff() {
                            myObject = (myObject) restTemplate.getForObject("url", myObject.class, "params");
                        }
                    
                        [...] 
                    }

                    Sample XML from the REST service:
                    Code:
                        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                        <myObject>
                            <something>word</something>
                            <somethingelse>word</somethingelse>
                        </myObject>


                    EDIT: I've updated spring and spring-oxm to 3.0.0.RELEASE, but the problem still occurs.
                    Last edited by Toxic; Dec 18th, 2009, 07:55 AM.

                    Comment


                    • #11
                      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>

                      Comment


                      • #12
                        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, 01:09 PM.

                        Comment


                        • #13
                          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>

                          Comment

                          Working...
                          X