參考資料:
利用wsdl2java工具生成webservice的客戶端代碼
一、wsimport生成
cmd命令示例:
wsimport -encoding utf-8 -s D:\Temp\TempCode -p com.wcf.proxy http://192.168.80.194:9191/UTPWService/metadata
上述命令報錯及解決方法:
1、[ERROR] 屬性 "Any" 已定義。請使用 <jaxb:property> 解決此沖突。
解決方法:
1)創建xsd.xjb文件(創建txt文檔,將下面內容復制進去,再更改文件名稱問xsd.xjb)

<?xml version="1.0" encoding="UTF-8"?> <bindings xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" version="2.0"> <globalBindings> <xjc:simple /> </globalBindings> <bindings scd="~xsd:complexType"> <class name="ComplexTypeType"/> </bindings> <bindings scd="~xsd:simpleType"> <class name="SimpleTypeType"/> </bindings> <bindings scd="~xsd:group"> <class name="GroupType"/> </bindings> <bindings scd="~xsd:attributeGroup"> <class name="AttributeGroupType"/> </bindings> <bindings scd="~xsd:element"> <class name="ElementType"/> </bindings> <bindings scd="~xsd:attribute"> <class name="attributeType"/> </bindings> </bindings>
命令中增加參數:-b http://www.w3.org/2001/XMLSchema.xsd -b xsd.xjb
下面以xsd.xjb放在目錄“D:Temp”為例:
wsimport -encoding utf-8 -s D:\Temp\TempCode -p com.wcf.proxy -b http://www.w3.org/2001/XMLSchema.xsd -b D:\Temp\xsd.xjb http://192.168.80.194:9191/UTPWService/metadata
2、[WARNING] 服務 "UTPWService" 不包含任何可用端口。請嘗試運行帶 -extension 開關的 wsimport。
上述提示表示需增加參數: -extension,否則生成的代碼可能沒有Service文件。
生成代碼的完整命令:
wsimport -extension -encoding utf-8 -s D:\Temp\TempCode -p com.wcf.proxy -b http://www.w3.org/2001/XMLSchema.xsd -b D:\Temp\xsd.xjb http://192.168.80.194:9191/UTPWService/metadata
3、測試代碼:

public static void main(String[] args) { try { // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); // // Date date = new Date(1590544075151L); // String res = simpleDateFormat.format(date); URL url = new URL("http://192.168.80.189:9191/UTPWService/metadata?wsdl"); UTPWService_Service utpwService = new UTPWService_Service(url); String input = "[{\"PointNo\":1,\"Statue\":0,\"UpdateTime\":\"2020-05-25T14:12:04.572756+08:00\"},{\"PointNo\":2,\"Statue\":1,\"UpdateTime\":\"2020-05-25T14:12:04.572756+08:00\"},{\"PointNo\":3,\"Statue\":0,\"UpdateTime\":\"2020-05-25T14:12:04.572756+08:00\"}]"; UTPWService service = utpwService.getWSHttpBindingUTPWService(new javax.xml.ws.soap.AddressingFeature()); String result = service.uploadDeviceStatus("", input); System.out.println(result); } catch(Exception e) { System.out.println(e.getMessage()); } }
注意:下面代碼會報錯:MustUnderstand headers:[{http://www.w3.org/2005/08/addressing}Action] are not understood
IFooService service = new FooService().getWSHttpBindingIFooService();
正確寫法為:
IFooService service = new FooService().getWSHttpBindingIFooService(new javax.xml.ws.soap.AddressingFeature());
參考資料:WCF Web Service, Java Web Client, MustUnderstand headers not understood?