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

发布于 2024-10-10 12:17:15 字数 6069 浏览 4 评论 0原文

我已经编写了 spring webservice cleint 示例,这与 springws1.5 一起工作正常,但是当使用 spring ws 1.0.4 运行时,我遇到了异常。请帮助我了解为什么我们会遇到此异常以及如何解决。我只需要使用 down 版本,因为我的 spring 版本是 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 批处理文件生成类, 我已将 jar 保存

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。我仍然面临这个问题,以前它有效。我没有做太多改变。我的代码有什么问题吗?如何检查..如何跟踪这个。非常感谢任何帮助。 我的 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();
    }

}

输入 以下是类级别注释

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

响应:

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

I have written spring webservice cleint example This is working fine with springws1.5 but when running with spring ws 1.0.4 i am getting below exception. Please help me why we will get this exception and how to resolve. I need to use down version only as my spring version is 2.0.4. Please reply as soon as possible.

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)

I have coded using following example
http://justcompiled.blogspot.com/2010/11/web-service-client-with-spring-ws.html
The following changes i have done:
Genereate classes using wsconsume batch file from jboss bin,
I have kept jars

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 

in jboss/../lib

Following is in my spring configuration file:

<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>

Now i have replaced everything and place webservice 1.5.0 jar. Stil i am facing this problem, Previously it worked. I haven't done much chanegs. is any thing disturbed in my code? How to chesk ..How to track this. Any help is highly appreciated.
My ObjectFactory is simple:

@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();
    }

}

Input
Following are classlevel annotations

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

Response:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴 2024-10-17 12:17:15

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

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

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

无论您使用什么工具来生成 JAXB 类,应该生成一个声明名称空间的 package-info.java 文件。你确定它不在某个地方吗?

The exception you're getting is telling you that the JAXB context is expecting to receive elements with no XML namespace. It's expecting this because your JAXB-annotated classes do not have a namespace defined anywhere. You'd expect to find this declared as @XmlNamespace, somewhere in the bound classes (or in package-info.java), but you don't have that anywhere.

Spring-WS, however, is receiving responses that do have a namespace (the http://yyy.org in your example), and so the unmarshalling fails.

What I can't explain is how this ever worked with Spring-WS 1.5.x - the namespace mismatch should fail for every version.

Whatever tool you used to generate the JAXB classes should have produced a package-info.java file that declared the namespace. Are you sure it's not in there somewhere?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文