CXF 入门示例
package com.hoo.service; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; /** * class: CXF WebService服务端helloWorld示例 * * @author e421083458 * @createDate 2013-5-28下午4:56:58 * @file HelloWorldService.java * @package com.hoo.service * @project cxf_demo01 */ @WebService @SOAPBinding(style = Style.RPC) public class HelloWorldService { public String sayHello(@WebParam(name = "name") String name){ return name + "say : Hello World"; } }
package com.hoo.service.deploy; import javax.xml.ws.Endpoint; import com.hoo.service.HelloWorldService; /** * class: 发布CXF WebService * * @author e421083458 * @createDate 2013-5-28下午5:01:05 * @file DeployHelloWorldService.java * @package com.hoo.service.deploy * @project cxf_demo01 */ public class DeployHelloWorldService { /** * function: 发布WebService * @author e421083458 */ public static void deployService(){ System.out.println("Service start......"); HelloWorldService service = new HelloWorldService(); String address = "http://localhost:9000/helloWorld"; Endpoint.publish(address, service); } public static void main(String args[])throws InterruptedException{ //发布WebService deployService(); System.out.println("server ready......"); Thread.sleep(1000*120); System.out.println("System exiting..."); //休眠60秒后退出 System.exit(0); } }
package com.hoo.service; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorldService { public String sayHello(@WebParam(name = "name") String name); }
package com.hoo.client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.hoo.service.IHelloWorldService; public class HelloWorldServiceClient { public static void main(String args[]){ //调用WebService JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(IHelloWorldService.class); factory.setAddress("http://localhost:9000/helloWorld"); IHelloWorldService service = (IHelloWorldService) factory.create(); System.out.println("[result]"+service.sayHello("e421083458")); } }
本教程改编自:http://www.cnblogs.com/hoojo/category/276011.html