Java使用cxf發(fā)布及調(diào)用webservice接口的方法詳解
一、cxf發(fā)布webservice接口
添加maven依賴(lài)
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.4.3</version>
</dependency>這里的cxf-rt-transports-http-jetty依賴(lài)是內(nèi)置jetty容器發(fā)布webservice接口
創(chuàng)建服務(wù)端接口
@WebService
public interface UserService {
String getUserInfo(String name);
}@WebService
public class UserServiceImpl implements UserService {
@Override
public String getUserInfo(String name) {
return "我是" + name;
}
}public class test {
public static void main(String[] args) {
System.out.println("web service start");
UserService implementor = new UserServiceImpl();
String address = "http://127.0.0.1:8082/userService";
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setAddress(address); // 設(shè)置暴露地址
factoryBean.setServiceClass(UserService.class); // 接口類(lèi)
factoryBean.setServiceBean(implementor); // 設(shè)置實(shí)現(xiàn)類(lèi)
factoryBean.create();
System.out.println("web service started");
System.out.println("web service started");
}
}啟動(dòng)main方法,正常情況會(huì)發(fā)布成功
瀏覽器訪(fǎng)問(wèn)http://127.0.0.1:8082/userService?wsdl 返回下面內(nèi)容說(shuō)明發(fā)布成功

二、客戶(hù)端進(jìn)行動(dòng)態(tài)調(diào)用webservice接口
public static void main(String[] args) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8082/userService?wsdl");
// namespace是命名空間,methodName是方法名
QName name = new QName("http://service.test.example.com/", "getUserInfo");
Object[] objects = client.invoke(name, "我沒(méi)收到反饋");
System.out.println(objects[0].toString());
}正常情況調(diào)用接口會(huì)返回成功
cxf的動(dòng)態(tài)調(diào)用不適合傳遞自定義對(duì)象,需要傳遞自定義對(duì)象,需要使用靜態(tài)調(diào)用生成本地代碼
三、cxf靜態(tài)調(diào)用生成本地代碼
采用apache-cxf-3.4.3版本,jar包可到官網(wǎng)下載 http://cxf.apache.org/download.html
使用JAX-WS Proxy代理工廠(chǎng)方式開(kāi)發(fā)客戶(hù)端,首先需要使用wsdl2java工具生成web service在本地的代理接口
1、配置環(huán)境變量
wsdl2java.bat在CXF根目錄\bin目錄下,使用之前需要配置CXF環(huán)境變量,CXF_HOME變量值為CXF根目錄,并在path中添加%CXF_HOME%\bin,如:


2、wsdl2java生成代理接口配置好環(huán)境變量后,打開(kāi)CMD命令行窗口,將當(dāng)前目錄切換到j(luò)ava工程src目錄下,命令行輸入如下格式命令,回車(chē),即可根據(jù)wsdl生成服務(wù)端的代理接口。
wsdl2java -p ws.cxf -client http://127.0.0.1:8082/userService?wsdl

命令說(shuō)明:
-p 指定代理接口所在包名,-client(生成客戶(hù)端) 后面的字符串聲明了服務(wù)接口的wsdl
public static void test() {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(UserService.class);
factory.setAddress("http://127.0.0.1:8082/userService");
Object object = factory.create();
UserService service = (UserService) object;
UserVO userVO = new UserVO();
userVO.setName("李師傅打開(kāi)");
String userInfo = service.getUserInfo(userVO);
System.out.println(userInfo);
}3、第二種調(diào)用方法
ExecuteBindQSService executeBindQSService = new ExecuteBindQSService(ExecuteBindQSService.getWsdlLocation(WSDL_URL));
ExecutePtt executePtt = executeBindQSService.getExecuteBindQSPort();
//獲取代理,并設(shè)置日志攔截器,打印日志
Client client = ClientProxy.getClient(executePtt);
client.getInInterceptors().add(new LoggingInInterceptor());
client.getOutInterceptors().add(new LoggingOutInterceptor());
// 超時(shí)時(shí)間設(shè)置
HTTPConduit http = (HTTPConduit) clientProxy.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(Integer
.valueOf(webServiceConTimeout));
httpClientPolicy.setReceiveTimeout(Integer
.valueOf(webServiceRevTimeout));
httpClientPolicy.setAllowChunking(false);
http.setClient(httpClientPolicy);
//調(diào)用方法執(zhí)行
Response response = executePtt.execute(request);4、關(guān)閉元素校驗(yàn)
@EndpointProperty(key = “soap.no.validate.parts”, value = “true”)

四、使用的是apache的axis實(shí)現(xiàn)的
第二種訪(fǎng)問(wèn)webservice的方法是通過(guò)idea生成客戶(hù)端代碼,訪(fǎng)問(wèn)服務(wù)端接口向訪(fǎng)問(wèn)本地接口一樣。使用的是apache的axis實(shí)現(xiàn)的。
以上就是Java使用cxf發(fā)布及調(diào)用webservice接口的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Java 使用cxf調(diào)用webservice接口的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java基于Scanner對(duì)象的簡(jiǎn)單輸入計(jì)算功能示例
這篇文章主要介紹了Java基于Scanner對(duì)象的簡(jiǎn)單輸入計(jì)算功能,結(jié)合實(shí)例形式分析了Java使用Scanner對(duì)象獲取用戶(hù)輸入半徑值計(jì)算圓形面積功能,需要的朋友可以參考下2018-01-01
Postman form-data、x-www-form-urlencoded的區(qū)別及說(shuō)明
這篇文章主要介紹了Postman form-data、x-www-form-urlencoded的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Spring Cloud Gateway網(wǎng)關(guān)XSS過(guò)濾方式
這篇文章主要介紹了Spring Cloud Gateway網(wǎng)關(guān)XSS過(guò)濾方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
java阻塞隊(duì)列BlockingQueue詳細(xì)解讀
這篇文章主要介紹了java阻塞隊(duì)列BlockingQueue詳細(xì)解讀,在新增的Concurrent包中,BlockingQueue很好的解決了多線(xiàn)程中,如何高效安全“傳輸”數(shù)據(jù)的問(wèn)題,通過(guò)這些高效并且線(xiàn)程安全的隊(duì)列類(lèi),為我們快速搭建高質(zhì)量的多線(xiàn)程程序帶來(lái)極大的便利,需要的朋友可以參考下2023-10-10
spring AOP的Around增強(qiáng)實(shí)現(xiàn)方法分析
這篇文章主要介紹了spring AOP的Around增強(qiáng)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了spring面向切面AOP的Around增強(qiáng)具體步驟與相關(guān)操作方法,需要的朋友可以參考下2020-01-01
淺談Java中replace與replaceAll區(qū)別
這篇文章主要介紹了Java中replace與replaceAll區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Spring Boot 基于注解的 Redis 緩存使用詳解
本篇文章主要介紹了Spring Boot 基于注解的 Redis 緩存使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
詳解Java并發(fā)包中線(xiàn)程池ThreadPoolExecutor
ThreadPoolExecutor是Java語(yǔ)言對(duì)于線(xiàn)程池的實(shí)現(xiàn)。線(xiàn)程池技術(shù)使線(xiàn)程在使用完畢后不回收而是重復(fù)利用。如果線(xiàn)程能夠復(fù)用,那么我們就可以使用固定數(shù)量的線(xiàn)程來(lái)解決并發(fā)問(wèn)題,這樣一來(lái)不僅節(jié)約了系統(tǒng)資源,而且也會(huì)減少線(xiàn)程上下文切換的開(kāi)銷(xiāo)2021-06-06
Spark學(xué)習(xí)筆記之Spark中的RDD的具體使用
這篇文章主要介紹了Spark學(xué)習(xí)筆記之Spark中的RDD的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

