Java如何調(diào)用wsdl的webservice接口
java開發(fā),當我們獲取到了對方提供的wsdl地址,然后在網(wǎng)頁上能夠訪問wsdl文檔以后,如何調(diào)用對方的webservic呢?
一.首先了解下WSDL
WSDL(網(wǎng)絡(luò)服務(wù)描述語言,Web Services Description Language)是一門基于 XML 的語言,用于描述 Web Services 以及如何對它們進行訪問。

二.如何生成webService客戶端去調(diào)用服務(wù)端
1.報存wsdl的xml文件:并將其后綴改為wsdl

2.把保存的 wsdl 文件加進項目,創(chuàng)建一個包,放在下面.

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());
// 請在此處添加業(yè)務(wù)代碼
NotifyTodoContext context = new NotifyTodoContext();
//數(shù)據(jù)格式要參考對方給的數(shù)據(jù)格式
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());
}
}
/**
* 調(diào)用服務(wù),生成客戶端的服務(wù)代理
*
* @param address WebService的URL
* @param serviceClass 服務(wù)接口全名
* @return 服務(wù)代理對象
* @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());
// 添加消息頭驗證信息。如果服務(wù)端要求驗證用戶密碼,請加入此段代碼
// factory.getOutInterceptors().add(new AddSoapHeader());
factory.setServiceClass(serviceClass);
factory.setAddress(address);
// 使用MTOM編碼處理消息。如果需要在消息中傳輸文檔附件等二進制內(nèi)容,請加入此段代碼
// Map props = new HashMap();
// props.put("mtom-enabled", Boolean.TRUE);
// factory.setProperties(props);
// 創(chuàng)建服務(wù)代理并返回
return factory.create();
}不同的環(huán)境(開發(fā)\測試\生產(chǎn))注意修改 IP 和端口

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring cloud Gateway簡介及相關(guān)配置方法
這篇文章主要介紹了Spring cloud Gateway簡介及相關(guān)配置方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04

