博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java调用.net的webservice接口
阅读量:4671 次
发布时间:2019-06-09

本文共 4153 字,大约阅读时间需要 13 分钟。

要调用webservice,首先得有接口,用已经写好的接口地址在myEclipse的目标project中,右键->new web service client->

选择JAX-WS方式,点击“next”,看到以下界面:输入webservice接口地址,然后选择你要生成客户端的package包,不选择默认是项目的default包。

点击“next”,进入验证环节,再次点击“next”,进入以下界面:如果在新建项目时new的是web service project,这两项不需要勾选,否则要勾选上。

最后点击“finish”完成。这样就生成了webservice的客户端。

接下来,在目标project程序中调用.net的webservice接口,推荐的方式是在资源文件中配置adapterDomain(等于wsdlLocation值),namespaceURI(等于targetNamespace的值),localPart(等于service的name)三个变量,这样是为了程序的可移植性更好,例如:在default.properties文件中定义这三个变量:

adapterDomain=http://192.168.1.104/ssd/AdapterService/AdapterService.asmx?wsdlnamespaceURI=http://tempuri.org/localPart=AdapterService

需要声明的是,这三个变量可以在http://192.168.1.104/ssd/AdapterService/AdapterService.asmx?wsdl这个接口地址或者生成的客户端的注解为@WebServiceClient的类中找到。

然后,在本项目新建一个CallWebService类:

1 public class CallWebservice { 2     static Logger logger = Logger.getLogger(CallWebservice.class.getName()); 3      4     private String adapterDomain; 5     private String namespaceURI; 6     private String localPart; 7      8     public CallWebservice(String adapterDomain,String namespaceURI,String localPart) { 9         super();10         this.adapterDomain = adapterDomain;11         this.namespaceURI = namespaceURI;12         this.localPart = localPart;13     }14 public AdapterServicePortBinding initPortBind() throws Exception{15         AdapterServicePortBinding adapterService = null;16         URL url;17         url = new URL(adapterDomain);18         QName qName = new QName(namespaceURI,localPart);19         javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qName);20         adapterService = service.getPort(AdapterServicePortBinding.class);21         return adapterService;22     }23     24 25     public String getAll() throws Exception {26         return initPortBind().getAll();27     }

AdapterServicePortBinding 是暴露了接口方法的类,不同的写法可能有不同的结果。这里方法getAll()是在接口中声明的方法,且在AdapterServicePortBinding 类中发布到网络中的。

这时,我们就可以在程序中调用这个接口的方法了。

但在这之前,要先写一个读取资源文件的util工具类:PropertiesUtil

1 public class PropertiesUtil { 2  3     static Logger logger = Logger.getLogger(PropertiesUtil.class.getName()); 4     private static PropertiesUtil instance = null; 5     private static String file = null; 6     private Properties props = new Properties(); 7     private static InputStream in = null; 8  9     public static synchronized PropertiesUtil getInstance() {10         if (instance == null) {11             instance = new PropertiesUtil();12         }13         return instance;14     }15 16     public PropertiesUtil() {17     }18 19     public void init(String inputFile) {20         if (!inputFile.equals(file)) {21             file = inputFile;22             try {23 //              in = new BufferedInputStream(new FileInputStream(inputFile));24                 in = PropertiesUtil.class.getClassLoader().getResourceAsStream(inputFile);25                 props.load(in);26             } catch (Exception e) {27                 logger.error("错误:配置文件操作失败, 初始化属性文件失败");28                 logger.error(e);29                 System.out.println("错误:配置文件操作失败, 初始化属性文件失败\n" + DateUtil.getInstance().formatterDate(new Date()) + " " + getClass().getName());30                 //e.printStackTrace();31             }32         }33     }34 35     public String readValue(String key) {36         String value = props.getProperty(key);37         return value;38     }

好了,现在可以来调用了。

例如,我在UserService类中这样写:

1 public String findAuthorTreeStr(){ 2        //调用方法获取CallWebservice 对象,并调用接口方法 3        List list = this.getService().getAll(); 4      5 } 6  7     /** 8      * 呼叫Webservice,获取实例 9      * @return10      */11 public CallWebservice getService(){12         PropertiesUtil propertiesUtil = PropertiesUtil.getInstance();13         propertiesUtil.init("default.properties");14         adapterDomain = propertiesUtil.readValue(adapterDomain);15         namespaceURI = propertiesUtil.readValue(namespaceURI);16         localPart = propertiesUtil.readValue(localPart);17         18         CallWebservice service = new CallWebservice(adapterDomain, namespaceURI, localPart);19         return service;20     }

这样就完成了一次webservice调用。笔记结束。

 

转载于:https://www.cnblogs.com/yeqrblog/p/4434088.html

你可能感兴趣的文章
python 处理CSV数据
查看>>
tensorflow实战系列(三)一个完整的例子
查看>>
Mybatis:resultMap的使用总结
查看>>
使用U盘安装Ubuntu
查看>>
XFTP 乱码
查看>>
java Int数据工具类
查看>>
下载文件根据浏览器判断文件名,解决兼容性问题
查看>>
当网站不允许上传ASP,CGI,CER等脚本文件时
查看>>
Access 中数据库操作时提示from子句语法错误
查看>>
【备战NOIP】[算法总结] 二分查找
查看>>
sort函数用于vector向量的排序
查看>>
《算法》-- 总结
查看>>
El表达式
查看>>
leetcode-题8-3sum
查看>>
SQL-Server使用点滴(二-系统表)
查看>>
Djiango django跨域 cookie session
查看>>
vue通过webpack打包后怎么运行
查看>>
MYSQL中的日期转换
查看>>
在线修改Schema
查看>>
【学术篇】SDOI2008 仪仗队
查看>>