2011年2月3日

Metro引入WSDL时的定义冲突

在使用Metro的导入工具,引入基于Microsoft的WCF的WSDL时,偶尔会出现下面的错误,

[ERROR] Two declarations cause a collision in the ObjectFactory class.

[ERROR] (Related to above error) This is the other declaration.

原因主要在于数据的绑定,简单来说在<tagA>下面有<BC>,而<tagAB>下面有<C>,于是在代码生成时都被定义为tagABC,造成了定义声明的冲突。解决的办法,其实也不难,需要改变默认的命名规则,也就是使用xpath将其中的一个映射强制修改掉,同时也可以调整package的名字,示例如下。

wsimport -Xendorsed -extension -keep -Xnocompile -target 2.1 -b SomeService.xjb "http://HostName/SomeService.wsdl"

SomeService.xjb:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc">

    <jaxb:globalBindings
        generateElementClass="false"
        mapSimpleTypeDef="false"
        generateMixedExtensions="false"
        optionalProperty="primitive">
    </jaxb:globalBindings>

    <jaxb:bindings schemaLocation="http://Hostname/SomeService.xsd" node="/xs:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="name.own.defined.package" />
        </jaxb:schemaBindings>

        <jaxb:bindings node=".//xs:complexType[@name='Customer']/xs:sequence/xs:element[@name='Mobile']">
            <jaxb:factoryMethod name="MobileNum" />

        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>


数据绑定JAXB的参考文档:
JAXB Guide - Dealing with errors

没有评论:

发表评论