Apache CXF实现WebService发布和调用
第一種方法:不用導(dǎo)入cxf jars
服務(wù)端:
1、 新建Web工程
2、新建接口和實現(xiàn)類、測試類
?目錄結(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);}實現(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;}}測試類代碼:
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);} }運行測試類:訪問:http://localhost:8100/greeting?wsdl
客戶端:
?1、新建java工程 ?,配置CXF環(huán)境變量 (下載Apache CXF2.7 )
?2、CMD打開命令窗口,運行以下命令,生產(chǎn)客戶端代碼: wsdl2java.bat ?-p com.cxf.spting ?-client -encoding utf-8 -noAddressBinding ?http://localhost:8100/greeting?wsdl ?
拷貝到新建java工程的src文件下
運行GreetingServiceImpl_GreetingServiceImplPort_Client.java訪問webservice
?
第二種:
新建web工程 引入cxf依賴包(最小jar)
修改以上測試類代碼
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);}運行訪問
客戶端:按照第一種方法執(zhí)行。。。
?
另外兩種調(diào)用webservice的方法
新建工程 ------測試類 ? ----- ?接口:
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发布和调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全栈工程师的思考 | 步步进阶经验之谈
- 下一篇: kingshard--一个支持shard