JAXB解组异常:spring webservice 1.0.4时的意外元素

时间:2011-01-04 22:10:02

标签: spring spring-ws jaxb2

我已经写过spring webservice cleint示例这对springws1.5工作正常但是当使用spring ws 1.0.4运行时我得到的是异常。请帮助我,为什么我们会得到这个例外以及如何解决。我只需使用down版本,因为我的弹簧版本是2.0.4。请尽可能尽快回复。

Unhandled exception: 
    org.springframework.oxm.jaxb.JaxbUnmarshallingFailureException: JAXB unmarshalling exception: unexpected element (uri:"http://yyy.org", local:"xxxResponse"). Expected elements are <{}xxx>,<{}xxxResponse>; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"yyy.org", local:"xxxResponse"). Expected elements are <{}xxx>,<{}xxxResponse>
    Caused by: 
    javax.xml.bind.UnmarshalException: unexpected element (uri:"http://yyy.org", local:"xxxResponse"). Expected elements are <{}xxx>,<{}xxxResponse>
     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:603)
     at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:244)
     at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:239)
     at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1009)
     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:446)
     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:427)
     at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:71)
     at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137)
     at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:240)
     at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:123)
     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:314)
     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:297)
     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:107)
     at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:395)
     at org.springframework.ws.support.MarshallingUtils.unmarshal(MarshallingUtils.java:62)
     at org.springframework.ws.client.core.WebServiceTemplate$2.extractData(WebServiceTemplate.java:276)
     at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:417)
     at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:265)
     at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:253)
     at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:245)
     at encompass.mtm.webservices.Code1ServiceClient.getAddressCheckResponse(Code1ServiceClient.java:24)

我使用以下示例进行了编码 http://justcompiled.blogspot.com/2010/11/web-service-client-with-spring-ws.html 我做了以下更改: 使用jboss bin中的wsconsume批处理文件生成类, 我一直放着罐子

saaj1.3.jar, spring-ws-core-1.0.4.jar,spring-xml-1.0.4.jar
spring-oxm-tiger-1.0.4.jar,spring-oxm-1.0.4.jar 
jboss/../lib

中的

以下是我的spring配置文件:

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
        <property name="messageFactory">
            <bean class="com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl"/>
        </property>
    </bean>

    <bean id="test1ObjectFactory" class="test.webservices.ObjectFactory"/>
    <bean id="test1ServiceMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="test.webservices" />
    </bean>
    <bean id="test1ServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <constructor-arg ref="messageFactory" />
        <property name="marshaller" ref="test1ServiceMarshaller"></property>
        <property name="unmarshaller" ref="test1ServiceMarshaller"></property>
        <property name="messageSender">
            <bean
                class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
            </bean>
        </property>
        <property name="defaultUri" value="${webservice.url}" />
    </bean>
    <bean id="test1ServiceClient" class="test.webservices.test1ServiceClient">
        <constructor-arg ref="test1ServiceTemplate"></constructor-arg>
        <property name="test1ObjectFactory" ref="test1ObjectFactory"></property>
    </bean>

现在我已经更换了所有内容并放置webservice 1.5.0 jar。我正面临这个问题,以前它有用。我没有做太多的挑战。在我的代码中是否有任何干扰?如何chesk ..如何跟踪这个。任何帮助都非常感谢。 我的ObjectFactory很简单:

@XmlRegistry
public class ObjectFactory {

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: test.webservices
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link xxxResponse }
     * 
     */
    public XxxResponse createxxxResponse() {
        return new xxxResponse();
    }

    /**
     * Create an instance of {@link xxx }
     * 
     */
    public Xxx createXXX() {
        return new XXX();
    }

}

输入 以下是classlevel annotations

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "xxxData"
})
@XmlRootElement(name = "Xxx")

响应:

  @ XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "xxxResult"
    })
    @XmlRootElement(name = "XxxResponse")

1 个答案:

答案 0 :(得分:10)

您得到的异常是告诉您JAXB上下文期望接收没有XML命名空间的元素。它期待这一点,因为您的JAXB注释类没有在任何地方定义的命名空间。您希望在绑定类(或@XmlNamespace)中的某处找到此声明为package-info.java,但您没有在任何地方。

然而,Spring-WS正在接收具有命名空间的响应(在您的示例中为http://yyy.org),因此解组失败。

我无法解释的是Spring-WS 1.5.x是如何工作的 - 每个版本的命名空间不匹配都会失败。

用于生成JAXB类的任何工具应该生成一个声明命名空间的package-info.java文件。你确定它不在某处吗?