Java调用.NET的WCF服务

参考资料:

用java调用.net的wcf其实还是很简单的

wcf客户端调试工具的使用

利用wsdl2java工具生成webservice的客户端代码

生成webservice客户端的一些坑

java调用WCF服务

一、wsimport生成

cmd命令示例:

wsimport -encoding utf-8  -s  D:TempTempCode  -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:TempTempCode  -p com.wcf.proxy -b http://www.w3.org/2001/XMLSchema.xsd -b D:Tempxsd.xjb http://192.168.80.194:9191/UTPWService/metadata

2、[WARNING] 服务 "UTPWService" 不包含任何可用端口。请尝试运行带 -extension 开关的 wsimport。

上述提示表示需增加参数: -extension,否则生成的代码可能没有Service文件。

生成代码的完整命令:

wsimport -extension  -encoding utf-8  -s  D:TempTempCode  -p com.wcf.proxy -b http://www.w3.org/2001/XMLSchema.xsd -b D:Tempxsd.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?

原文地址:https://www.cnblogs.com/cheng2015/p/12971436.html