二、CXF 入门示例

又一HelloWorld示例,老套的HelloWorld是入门经典。也是基础和必须掌握的,下面看看HelloWorldWebService,很简单。

需要的jar包如下:

image

1、 HelloWorldService服务器端代码

package com.hoo.service;
 
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
 
/**
 * <b>function:</b>CXF WebService 服务器端helloWorld示例
 * @author hoojo
 * @createDate 2011-3-16 上午08:29:07
 * @file HelloWorld.java
 * @package com.hoo.service
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
@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	

还有就是如果jdk版本在1.6下,也会出现上面的异常。

解决办法有:

1> 先用apt命令将主程序进行编译,然后生成一些java文件

2> 升级JDK到版本jdk1.6u17或以后

2、 发布HelloWorldService,代码如下:

package com.hoo.service.deploy;

import javax.xml.ws.Endpoint;
import com.hoo.service.HelloWorldService;

/**
 * <b>function:</b> 发布CXF WebService
 * @author hoojo
 * @createDate 2011-3-17 上午09:24:33
 * @file DeployHelloWorldService.java
 * @package com.hoo.deploy
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
public class DeployHelloWorldService {
	
	/**
	 * <b>function:</b>发布WebService
	 * @author hoojo
	 */
	public static void deployService() {
		System.out.println("Server 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 * 60);
		System.out.println("server exiting");
		//休眠60秒后就退出
		System.exit(0);
	}
}

运行上面的main函数后,如果没有发现异常。基本信息是这样的:

Server start ……
2011-3-17 10:17:25 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.hoo.com/}HelloWorldServiceService from class com.hoo.service.HelloWorldService
2011-3-17 10:17:25 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:9000/helloWorld
2011-03-17 10:17:25.375:INFO::jetty-7.2.2.v20101205
2011-03-17 10:17:25.468:INFO::Started SelectChannelConnector@localhost:9000
server ready ……

那么你在WebBrowser中请求:

http://localhost:9000/helloWorld?wsdl就可以看到xml内容了。

3、 定制客户端调用WebService的接口,这个接口中的方法签名和参数信息可以从wsdl中的内容看到,代码如下:

package com.hoo.service;
 
import javax.jws.WebParam;
import javax.jws.WebService;
 
/**
 * <b>function:</b> 客户端调用WebService所需要的接口
 * @author hoojo
 * @createDate 2011-3-17 上午09:00:00
 * @file IHelloWorldService.java
 * @package com.hoo.service
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
@WebService
public interface IHelloWorldService {
    public String sayHello(@WebParam(name = "name") String name);
}

4、 编写客户端调用WebService代码

package com.hoo.client;
 
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.hoo.service.IHelloWorldService;
 
/**
 * <b>function:</b>CXF WebService客户端调用代码
 * @author hoojo
 * @createDate 2011-3-16 上午09:03:49
 * @file HelloWorldServiceClient.java
 * @package com.hoo.client
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
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("hoojo"));
    }
}

运行上面的程序后,结果如下:

2011-3-17 10:23:34 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.hoo.com/}IHelloWorldServiceService from class com.hoo.service.IHelloWorldService
[result]hoojo say: Hello World 
posted on 2011-03-29 15:55  hoojo  阅读(17925)  评论(19编辑  收藏  举报