java开发,当我们获取到了对方提供的wsdl地址,然后在网页上能够访问wsdl文档以后,如何调用对方的webservic呢?
一.首先了解下wsdl
wsdl(网络服务描述语言,web services description language)是一门基于 xml 的语言,用于描述 web services 以及如何对它们进行访问。
二.如何生成webservice客户端去调用服务端
1.报存wsdl的xml文件:并将其后缀改为wsdl
2.把保存的 wsdl 文件加进项目,创建一个包,放在下面.
3.使用idea自带插件,tools -> webservices -> generatte java code from wsdl (这里有坑,idea版本低于2020的 没有webservices)
生成如下图:
生成是会加入依赖:
<dependency> <groupid>org.apache.cxf</groupid> <artifactid>cxf-rt-frontend-jaxws</artifactid> <version>3.1.11</version> <scope>compile</scope> </dependency>
这里涉及到 spring整合cxf
三.客户端参考代码
public static void main(string[] args) throws exception { webserviceconfig cfg = webserviceconfig.getinstance(); isysnotifytodowebservice service = (isysnotifytodowebservice) callservice(cfg.getaddress(), cfg.getserviceclass()); // 请在此处添加业务代码 notifytodocontext context = new notifytodocontext(); //数据格式要参考对方给的数据格式 context.setsubject("测试待办webservice~~~"); context.setlink("http://news.sina.com.cn/"); context.settype(2); context.setkey("sinanews"); context.setmodelid("123456789"); context.settargets("{\"id\":\"12fe2de141b7b97b32d1af34204a9f54\"}"); context.setopttime("2022-01-25 09:25:09"); notifytodoappresult result = service.sendtodo(context); if (result != null) { if (result.getreturnstate() != 2) system.out.println(result.getmessage()); } } /** * 调用服务,生成客户端的服务代理 * * @param address webservice的url * @param serviceclass 服务接口全名 * @return 服务代理对象 * @throws exception */ public static object callservice(string address, class serviceclass) throws exception { jaxwsproxyfactorybean factory = new jaxwsproxyfactorybean(); // 记录入站消息 factory.getininterceptors().add(new loggingininterceptor()); // 记录出站消息 factory.getoutinterceptors().add(new loggingoutinterceptor()); // 添加消息头验证信息。如果服务端要求验证用户密码,请加入此段代码 // factory.getoutinterceptors().add(new addsoapheader()); factory.setserviceclass(serviceclass); factory.setaddress(address); // 使用mtom编码处理消息。如果需要在消息中传输文档附件等二进制内容,请加入此段代码 // map props = new hashmap(); // props.put("mtom-enabled", boolean.true); // factory.setproperties(props); // 创建服务代理并返回 return factory.create(); }
不同的环境(开发\测试\生产)注意修改 ip 和端口
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论