首页 > 技术文章 > Java中调用 .net中的webservice 接口

jtcr 2018-03-09 16:12 原文

使用wsimport命令生成webService客户端
wsimport简介

在JDK的bin文件夹中,有一个wsimport.exe工具,可依据wsdl文件生成相应的类文件,将生存在本地这些类文件拷贝到需要使用的项目中,就可以像调用本地的类一样调用webService提供的方法。该工具可以用于非Java的服务器,如用C#编写的WebService,通过wsimport则生成Java的客户端实现。
常用命令如下:

wsimport -keep -s D:\temp -p com.ttt.resource.bean
.webservice -verbose http://www.tt.com/tttttt/WebService/ExamInfoO
utService.asmx?wsdl

实际要进入JDK的bin里面执行上面的命令:例如:
D:\jdk1.8.0_141\bin>wsimport -keep -s D:\temp -p com.ttt.resource.bean
.webservice -verbose http://www.tt.com/tttttt/WebService/ExamInfoO
utService.asmx?wsdl

有时候在本地找不到wsdl,请在有域名的测试环境等进行生成

-keep:是否生成java源文件

-d:指定.class文件的输出目录

-s:指定.java文件的输出目录

-p:定义生成类的包名,不定义的话有默认包名
-verbose:在控制台显示输出信息

-b:指定jaxws/jaxb绑定文件或额外的schemas

-extension:使用扩展来支持SOAP1.2


当我们点击生成的几个java文件看时,是不是发现中文注释乱码了,如果想把源代码的编码变成UTF-8,可以下载一个工具:UTFCastExpress

http://download.csdn.net/detail/aqsunkai/9535166


编写测试类:
//创建一个ExamInfoOutService工厂
ExamInfoOutService examInfoOutService= new ExamInfoOutService();
//根据工厂创建一个ExamInfoOutServiceSoap对象
ExamInfoOutServiceSoap examInfoOutServiceSoap = examInfoOutService.getExamInfoOutServiceSoap();
////调用WebService提供的getExamPaperForUser方法查询试题
String strResult = examInfoOutServiceSoap.getExamPaperForUser(interFaceVo.getUserid(), interFaceVo.getExamid());
System.out.println(strResult);

推荐阅读