在http://cxf.apache.org/download.html下载CXF,然后加载它的所有jar包。
主要写四个类:接口、实现类、服务端、客户端
package com.cxf; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHi(@WebParam(name = "text") String text); public String sayHiToUser(User user); }
package com.cxf; import javax.jws.WebParam; import javax.jws.WebService; @WebService public class HelloWorldImpl implements HelloWorld { @Override public String sayHi(@WebParam(name = "text") String text) { return "Hello " + text; } @Override public String sayHiToUser(User user) { return "Hello " + user.getName(); } }
package com.cxf; public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.cxf; import javax.xml.ws.Endpoint; public class Server { /** * @param args */ public static void main(String[] args) { System.out.println("web service start"); HelloWorld implementor = new HelloWorldImpl(); String address = "http://localhost:8080/helloWorld"; Endpoint.publish(address, implementor); System.out.println("web service started"); } }
package com.cxf; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class Client { /** * @param args */ public static void main(String[] args) { JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(HelloWorld.class); svr.setAddress("http://localhost:8080/helloWorld"); HelloWorld hw = (HelloWorld) svr.create(); User user = new User(); user.setName("yang"); System.out.println(hw.sayHiToUser(user)); System.out.println(hw.sayHi("yang2")); } }
web service start 2014-1-28 21:40:10 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://cxf.com/}HelloWorldImplService from class com.cxf.HelloWorld 2014-1-28 21:40:11 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server's publish address to be http://localhost:8080/helloWorld 2014-1-28 21:40:11 org.eclipse.jetty.server.Server doStart 信息: jetty-8.1.14.v20131031 2014-1-28 21:40:11 org.eclipse.jetty.server.AbstractConnector doStart 信息: Started SelectChannelConnector@localhost:8080 2014-1-28 21:40:12 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL 信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl 2014-1-28 21:40:12 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702 2014-1-28 21:40:12 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl web service started
在浏览器中输入http://localhost:8080/helloWorld?wsdl,可以看到:
运行客户端:
2014-1-28 21:40:51 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://cxf.com/}HelloWorldService from class com.cxf.HelloWorld Hello yang Hello yang2