CXF-01 CXF入门

 

一、CXF WebService准备

CXF官网:http://cxf.apache.org/

从官网上下载CXF Jar,将下载下来的Jar包解压后,目录为:

1bin目录提供的是常用的dos控制台命令工具

2docs 帮助文档

3lib jar包、依赖库

4lib-samples 也是jar包,有jettyhttpclientsjunitjar

5modules 打包好的WebService模块

6samples示例demo

 

需要的Jar包如下:

CXF-01 CXF入门

 

 

 

 

 

 

 

 

1 HelloWorldService服务器端代码 

package com.iflytek.service;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
 * @author xdwang
 * 
 * @create Apr 25, 2013 10:23:54 PM
 * 
 * @email:[email protected]
 * 
 * @description CXF WebService 服务器端helloWorld示例
 * 
 */
@WebService
@SOAPBinding(style = Style.RPC)
public class HelloWorldService {
	public String sayHello(@WebParam(name = "name") String name) {
		return name + " say: Hello World ";
	}
}

 

要说明的是,上面的服务器端代码运用了JDK5的新特性Annotation,这个非常好用。

需要注意的是,如果不添加@SOAPBinding(style = Style.RPC)注解,将会出现异常:

 com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error         

 

2发布HelloWorldService

package com.iflytek.service.deploy;

import javax.xml.ws.Endpoint;

import com.iflytek.service.HelloWorldService;

/**
 * @author xdwang
 * 
 * @create Apr 25, 2013 10:28:13 PM
 * 
 * @email:[email protected]
 * 
 * @description 发布CXF WebService
 * 
 */
public class DeployHelloWorldService {
	
	/**
	 * @descrption 发布WebService
	 * @author xdwang
	 * @create Apr 25, 201310:28:45 PM
	 */
	public static void deployService() {
		System.out.println("Server start ……");
		HelloWorldService service = new HelloWorldService();
		String address = "http://localhost:8000/helloWorld";
		Endpoint.publish(address, service);
	}

	public static void main(String[] args) throws InterruptedException {
		// 发布WebService
		deployService();
		System.out.println("server ready ……");
		Thread.sleep(1000 * 60);
		System.out.println("server exiting");
		// 休眠60秒后就退出
		System.exit(0);
	}

}

 运行上面的main函数后在WebBrowser中请求:http://localhost:8000/helloWorld?wsdl,如果可以看到xml内容,说明发布成功了。

其中有会报

Exception in thread "main" org.apache.cxf.bus.extension.ExtensionException: Could not load extension class org.apache.cxf.ws.policy.AssertionBuilderRegistryImpl.
	at org.apache.cxf.bus.extension.Extension.tryClass(Extension.java:173)
	at org.apache.cxf.bus.extension.Extension.getClassObject(Extension.java:185)
	at org.apache.cxf.bus.extension.ExtensionManagerImpl.activateAllByType(ExtensionManagerImpl.java:138)
	at org.apache.cxf.bus.extension.ExtensionManagerBus.<init>(ExtensionManagerBus.java:126)
	at org.apache.cxf.bus.extension.ExtensionManagerBus.<init>(ExtensionManagerBus.java:134)
	at org.apache.cxf.bus.CXFBusFactory.createBus(CXFBusFactory.java:40)
	at org.apache.cxf.bus.CXFBusFactory.createBus(CXFBusFactory.java:36)
	at org.apache.cxf.bus.CXFBusFactory.createBus(CXFBusFactory.java:32)
	at org.apache.cxf.BusFactory.getDefaultBus(BusFactory.java:106)
	at org.apache.cxf.BusFactory.createThreadBus(BusFactory.java:216)
	at org.apache.cxf.BusFactory.getThreadDefaultBus(BusFactory.java:206)
	at org.apache.cxf.BusFactory.getThreadDefaultBus(BusFactory.java:193)
	at org.apache.cxf.jaxws.spi.ProviderImpl.createEndpoint(ProviderImpl.java:128)
	at org.apache.cxf.jaxws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:151)
	at javax.xml.ws.Endpoint.publish(Unknown Source)
	at com.iflytek.service.deploy.DeployHelloWorldService.deployService(DeployHelloWorldService.java:28)
	at com.iflytek.service.deploy.DeployHelloWorldService.main(DeployHelloWorldService.java:33)
Caused by: java.lang.NoClassDefFoundError: org/apache/neethi/AssertionBuilderFactory
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClassCond(Unknown Source)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.access$000(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClassCond(Unknown Source)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.access$000(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at org.apache.cxf.bus.extension.Extension.tryClass(Extension.java:169)
	... 16 more
Caused by: java.lang.ClassNotFoundException: org.apache.neethi.AssertionBuilderFactory
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 41 more

 

找了半天也没搞定,无奈之下将CXFlib下的所有jarcopy进去了,就没报了,想找出具体是缺失哪一个也没找到,唉。

  

3定制客户端调用WebService的接口

 这个接口中的方法签名和参数信息可以从wsdl中的内容看到,代码如下:

package com.iflytek.service;

import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * @author xdwang
 * 
 * @create Apr 25, 2013 10:30:38 PM
 * 
 * @email:[email protected]
 * 
 * @description 客户端调用WebService所需要的接口
 * 
 */
@WebService
public interface IHelloWorldService {
	public String sayHello(@WebParam(name = "name") String name);
}

 

4编写客户端调用WebService代码

package com.iflytek.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.iflytek.service.IHelloWorldService;

/**
 * @author xdwang
 * 
 * @create Apr 25, 2013 10:35:50 PM
 * 
 * @email:[email protected]
 * 
 * @description CXF WebService客户端调用代码
 * 
 */
public class HelloWorldServiceClient {
	public static void main(String[] args) { // 调用WebService
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(IHelloWorldService.class);
		factory.setAddress("http://localhost:8000/helloWorld");
		IHelloWorldService service = (IHelloWorldService) factory.create();
		System.out.println("[result]" + service.sayHello("xdwang"));
	}
}

 

运行结果:

2013-7-25 18:53:40 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.iflytek.com/}IHelloWorldServiceService from class com.iflytek.service.IHelloWorldService
[result]xdwang say: Hello World 

 

 

 

你可能感兴趣的:(java,webservice,CXF)