日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Apache CXF实现WebService发布和调用

發(fā)布時(shí)間:2025/3/20 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Apache CXF实现WebService发布和调用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一種方法:不用導(dǎo)入cxf jars

服務(wù)端:

1、 新建Web工程

2、新建接口和實(shí)現(xiàn)類、測(cè)試類

?目錄結(jié)構(gòu)圖如下:

接口代碼:

package com.cxf.spring.service;import javax.jws.WebMethod; import javax.jws.WebService;@WebService public interface IGreetingService {@WebMethodpublic String greeting(String name);}

實(shí)現(xiàn)類代碼:

package com.cxf.spring.service;import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService;@WebService public class GreetingServiceImpl implements IGreetingService{@WebMethod@Overridepublic String greeting(@WebParam(name ="name") String name) {return "HI:....."+name;}}

測(cè)試類代碼:

package com.cxf.spring.test;import javax.xml.ws.Endpoint;import com.cxf.spring.service.GreetingServiceImpl;public class TestSpring {public static void main(String[] args) {pushWS_1();}public static void pushWS_1() {String address = "http://localhost:8100/greeting";System.out.println("Server start....");Endpoint.publish(address, new GreetingServiceImpl());System.out.println("Server ready....");try {Thread.sleep(3*60*1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Server exit");System.exit(0);} }

運(yùn)行測(cè)試類:訪問(wèn):http://localhost:8100/greeting?wsdl

客戶端:

?1、新建java工程 ?,配置CXF環(huán)境變量 (下載Apache CXF2.7 )

?2、CMD打開命令窗口,運(yùn)行以下命令,生產(chǎn)客戶端代碼: wsdl2java.bat ?-p com.cxf.spting ?-client -encoding utf-8 -noAddressBinding ?http://localhost:8100/greeting?wsdl ?

拷貝到新建java工程的src文件下

運(yùn)行GreetingServiceImpl_GreetingServiceImplPort_Client.java訪問(wèn)webservice

?

第二種:

新建web工程 引入cxf依賴包(最小jar)

修改以上測(cè)試類代碼

package com.cxf.spring.test;import javax.xml.ws.Endpoint;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;import com.cxf.spring.service.GreetingServiceImpl; import com.cxf.spring.service.IGreetingService;public class TestSpring {public static void main(String[] args) {pushWS_2();}public static void pushWS_2() {JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();bean.setAddress("http://localhost:8100/greeting");bean.setServiceClass(IGreetingService.class);bean.setServiceBean(new GreetingServiceImpl());bean.create();System.out.println("Server ready....");try {Thread.sleep(3*60*1000);} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}System.out.println("Server exit");System.exit(0);}

運(yùn)行訪問(wèn)

客戶端:按照第一種方法執(zhí)行。。。

?

另外兩種調(diào)用webservice的方法

新建工程 ------測(cè)試類 ? ----- ?接口:

package com.cxf.test;import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import com.cxf.test.client.IGreetingService;public class TestClient_2 {public static void main(String[] args) { pull_1();pull_2();}public static void pull_1(){JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8100/greeting?wsdl"); try { Object[] objs = client.invoke("greeting", "張三"); System.out.println(objs[0].toString()); } catch (Exception e) { e.printStackTrace(); } }public static void pull_2() {JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(IGreetingService.class); factory.setAddress("http://localhost:8100/greeting?wsdl"); IGreetingService service = (IGreetingService) factory.create(); System.out.println(">>>>>>>>Client: " + service.greeting("戰(zhàn)士"));} } package com.cxf.test.client;import javax.jws.WebMethod; import javax.jws.WebService;@WebService(targetNamespace="http://service.spring.cxf.com/", name = "IGreetingService") public interface IGreetingService {@WebMethodpublic String greeting(String name);}

?

轉(zhuǎn)載于:https://www.cnblogs.com/liangblog/p/4655056.html

總結(jié)

以上是生活随笔為你收集整理的Apache CXF实现WebService发布和调用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。