其實不是很喜好這種複雜格式WebService,因為一旦複雜各平台與套件支援的程度就不一,簡單說就容易有坑啦,處理起來就累人了,反正偶爾還是會碰到就把處理方式記錄下來。
先到Apache CXF來下載CXF
政府有一個 政府資料開放平臺 裡面就有很多公開資料可以利用,就拿其中一個來練習,我是拿 中油主產品牌價 這服務來練習。
底下是原本的WSDL,因為.net的WSDL跟CXF處理上有些落差,等下還要手動調整。
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tmtd.cpc.com.tw/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://tmtd.cpc.com.tw/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tmtd.cpc.com.tw/">
<s:element name="getCPCMainProdListPrice">
<s:complexType />
</s:element>
<s:element name="getCPCMainProdListPriceResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getCPCMainProdListPriceResult">
<s:complexType>
<s:sequence>
<s:element ref="s:schema" />
<s:any />
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getCPCMainProdListPrice_Historical">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="prodid" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getCPCMainProdListPrice_HistoricalResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getCPCMainProdListPrice_HistoricalResult">
<s:complexType>
<s:sequence>
<s:element ref="s:schema" />
<s:any />
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getCPCAviationFuelListPrice">
<s:complexType />
</s:element>
<s:element name="getCPCAviationFuelListPriceResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getCPCAviationFuelListPriceResult">
<s:complexType>
<s:sequence>
<s:element ref="s:schema" />
<s:any />
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getCPCMarineFuelListPrice">
<s:complexType />
</s:element>
<s:element name="getCPCMarineFuelListPriceResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getCPCMarineFuelListPriceResult">
<s:complexType>
<s:sequence>
<s:element ref="s:schema" />
<s:any />
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getCPCMarineFuelListPrice_International">
<s:complexType />
</s:element>
<s:element name="getCPCMarineFuelListPrice_InternationalResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getCPCMarineFuelListPrice_InternationalResult">
<s:complexType>
<s:sequence>
<s:element ref="s:schema" />
<s:any />
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getCPCMainProdListPrice_English">
<s:complexType />
</s:element>
<s:element name="getCPCMainProdListPrice_EnglishResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getCPCMainProdListPrice_EnglishResult">
<s:complexType>
<s:sequence>
<s:element ref="s:schema" />
<s:any />
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="getCPCMainProdListPriceSoapIn">
<wsdl:part name="parameters" element="tns:getCPCMainProdListPrice" />
</wsdl:message>
<wsdl:message name="getCPCMainProdListPriceSoapOut">
<wsdl:part name="parameters" element="tns:getCPCMainProdListPriceResponse" />
</wsdl:message>
<wsdl:message name="getCPCMainProdListPrice_HistoricalSoapIn">
<wsdl:part name="parameters" element="tns:getCPCMainProdListPrice_Historical" />
</wsdl:message>
<wsdl:message name="getCPCMainProdListPrice_HistoricalSoapOut">
<wsdl:part name="parameters" element="tns:getCPCMainProdListPrice_HistoricalResponse" />
</wsdl:message>
<wsdl:message name="getCPCAviationFuelListPriceSoapIn">
<wsdl:part name="parameters" element="tns:getCPCAviationFuelListPrice" />
</wsdl:message>
<wsdl:message name="getCPCAviationFuelListPriceSoapOut">
<wsdl:part name="parameters" element="tns:getCPCAviationFuelListPriceResponse" />
</wsdl:message>
<wsdl:message name="getCPCMarineFuelListPriceSoapIn">
<wsdl:part name="parameters" element="tns:getCPCMarineFuelListPrice" />
</wsdl:message>
<wsdl:message name="getCPCMarineFuelListPriceSoapOut">
<wsdl:part name="parameters" element="tns:getCPCMarineFuelListPriceResponse" />
</wsdl:message>
<wsdl:message name="getCPCMarineFuelListPrice_InternationalSoapIn">
<wsdl:part name="parameters" element="tns:getCPCMarineFuelListPrice_International" />
</wsdl:message>
<wsdl:message name="getCPCMarineFuelListPrice_InternationalSoapOut">
<wsdl:part name="parameters" element="tns:getCPCMarineFuelListPrice_InternationalResponse" />
</wsdl:message>
<wsdl:message name="getCPCMainProdListPrice_EnglishSoapIn">
<wsdl:part name="parameters" element="tns:getCPCMainProdListPrice_English" />
</wsdl:message>
<wsdl:message name="getCPCMainProdListPrice_EnglishSoapOut">
<wsdl:part name="parameters" element="tns:getCPCMainProdListPrice_EnglishResponse" />
</wsdl:message>
<wsdl:portType name="ListPriceWebServiceSoap">
<wsdl:operation name="getCPCMainProdListPrice">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 中油汽柴燃油 零售牌價 DataSet</wsdl:documentation>
<wsdl:input message="tns:getCPCMainProdListPriceSoapIn" />
<wsdl:output message="tns:getCPCMainProdListPriceSoapOut" />
</wsdl:operation>
<wsdl:operation name="getCPCMainProdListPrice_Historical">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">1:無鉛汽油92, 2:無鉛汽油95, 3:無鉛汽油98, 4:超級/高級柴油, 5:低硫燃料油(0.5%), 6:甲種低硫燃料油(0.5) --- 中油汽柴燃油 歷史零售牌價 DataSet</wsdl:documentation>
<wsdl:input message="tns:getCPCMainProdListPrice_HistoricalSoapIn" />
<wsdl:output message="tns:getCPCMainProdListPrice_HistoricalSoapOut" />
</wsdl:operation>
<wsdl:operation name="getCPCAviationFuelListPrice">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 中油航空燃油 牌價 DataSet</wsdl:documentation>
<wsdl:input message="tns:getCPCAviationFuelListPriceSoapIn" />
<wsdl:output message="tns:getCPCAviationFuelListPriceSoapOut" />
</wsdl:operation>
<wsdl:operation name="getCPCMarineFuelListPrice">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 中油海運燃油(國內) 牌價 DataSet</wsdl:documentation>
<wsdl:input message="tns:getCPCMarineFuelListPriceSoapIn" />
<wsdl:output message="tns:getCPCMarineFuelListPriceSoapOut" />
</wsdl:operation>
<wsdl:operation name="getCPCMarineFuelListPrice_International">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 中油海運燃油(International) 牌價 DataSet</wsdl:documentation>
<wsdl:input message="tns:getCPCMarineFuelListPrice_InternationalSoapIn" />
<wsdl:output message="tns:getCPCMarineFuelListPrice_InternationalSoapOut" />
</wsdl:operation>
<wsdl:operation name="getCPCMainProdListPrice_English">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 中油汽柴油 零售牌價 DataSet <英文版></wsdl:documentation>
<wsdl:input message="tns:getCPCMainProdListPrice_EnglishSoapIn" />
<wsdl:output message="tns:getCPCMainProdListPrice_EnglishSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ListPriceWebServiceSoap" type="tns:ListPriceWebServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getCPCMainProdListPrice">
<soap:operation soapAction="http://tmtd.cpc.com.tw/getCPCMainProdListPrice" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCMainProdListPrice_Historical">
<soap:operation soapAction="http://tmtd.cpc.com.tw/getCPCMainProdListPrice_Historical" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCAviationFuelListPrice">
<soap:operation soapAction="http://tmtd.cpc.com.tw/getCPCAviationFuelListPrice" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCMarineFuelListPrice">
<soap:operation soapAction="http://tmtd.cpc.com.tw/getCPCMarineFuelListPrice" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCMarineFuelListPrice_International">
<soap:operation soapAction="http://tmtd.cpc.com.tw/getCPCMarineFuelListPrice_International" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCMainProdListPrice_English">
<soap:operation soapAction="http://tmtd.cpc.com.tw/getCPCMainProdListPrice_English" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="ListPriceWebServiceSoap12" type="tns:ListPriceWebServiceSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getCPCMainProdListPrice">
<soap12:operation soapAction="http://tmtd.cpc.com.tw/getCPCMainProdListPrice" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCMainProdListPrice_Historical">
<soap12:operation soapAction="http://tmtd.cpc.com.tw/getCPCMainProdListPrice_Historical" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCAviationFuelListPrice">
<soap12:operation soapAction="http://tmtd.cpc.com.tw/getCPCAviationFuelListPrice" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCMarineFuelListPrice">
<soap12:operation soapAction="http://tmtd.cpc.com.tw/getCPCMarineFuelListPrice" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCMarineFuelListPrice_International">
<soap12:operation soapAction="http://tmtd.cpc.com.tw/getCPCMarineFuelListPrice_International" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getCPCMainProdListPrice_English">
<soap12:operation soapAction="http://tmtd.cpc.com.tw/getCPCMainProdListPrice_English" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ListPriceWebService">
<wsdl:port name="ListPriceWebServiceSoap" binding="tns:ListPriceWebServiceSoap">
<soap:address location="https://vipmember.tmtd.cpc.com.tw/OpenData/ListPriceWebService.asmx" />
</wsdl:port>
<wsdl:port name="ListPriceWebServiceSoap12" binding="tns:ListPriceWebServiceSoap12">
<soap12:address location="https://vipmember.tmtd.cpc.com.tw/OpenData/ListPriceWebService.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
接下來照一般程序來執行WSDL轉JavaBean
看到上面結果你應該就會跟我一樣脫口WTF!!
這不是中油的WSDL有錯,也不是Apache在搞你啦,估計應是兩邊對命名空間的差異造成的
.net產出WSDL的命名空間是 xmlns:s="http://www.w3.org/2001/XMLSchema"
CXF產生的WSDL命名空間是 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
這應該就是原因了
解法一是
wsdl2java -b http://www.w3.org/2001/XMLSchema.xsd -d D:\test\src -encoding utf-8 -all https://vipmember.tmtd.cpc.com.tw/OpenData/ListPriceWebService.asmx?WSDL
不過這樣就會連XMLSchema.xsd都一起產生出來啦,不過使用上應該是沒差,XMLSchema那些可以殺掉
另外解法就是
下載WSDL,刪掉所有
<s:element ref="s:schema" />
再重新執行就可以正常生成JavaBean了
順便記錄下DOS指令
set Path=%Path%;D:\IDE\cxf\apache-cxf-2.7.10\bin
set CXF_HOME=D:\IDE\cxf\apache-cxf-2.7.10
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_51
wsdl2java -d D:\test\src -encoding utf-8 -all D:\test\ListPriceWebService.xml
wsdl2java參數說明
-p 指定其wsdl的命名空間,也就是要生成代碼的包名:
-d 指定要產生代碼所在目錄
-client 生成客戶端測試web service的代碼
-server 生成服務器啟動web service的代碼
-impl 生成web service的實現代碼
-ant 生成build.xml文件
-all 生成所有開始端點代碼:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
-encoding 輸出檔案時採用編碼
參考來源
http://blog.csdn.net/lgh1117/article/details/7771876
https://weblogs.java.net/blog/vivekp/archive/2007/05/how_to_deal_wit_1.html