第一种方法:不用导入cxf jars
服务端:
1、 新建Web工程
2、新建接口和实现类、测试类
目录结构图如下:
接口代码:
package com.cxf.spring.service; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface IGreetingService { @WebMethod public String greeting(String name); }
实现类代码:
package com.cxf.spring.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public class GreetingServiceImpl implements IGreetingService{ @WebMethod @Override public 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环境变量 (下载Apache CXF2.7 )
2、CMD打开命令窗口,运行以下命令,生产客户端代码: 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); }
运行访问
客户端:按照第一种方法执行。。。
另外两种调用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("战士")); } }
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 { @WebMethod public String greeting(String name); }
时间: 2024-11-04 04:46:58