Java調用.NET的WCF服務


參考資料:

用java調用.net的wcf其實還是很簡單的

wcf客戶端調試工具的使用

利用wsdl2java工具生成webservice的客戶端代碼

生成webservice客戶端的一些坑

java調用WCF服務

 

一、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>
View Code

命令中增加參數:-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());
        }
        
    }
View Code

 

注意:下面代碼會報錯: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?

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM