CXF(2.7.10)利用Spring发布Web服务的简单示例

1. 新建工程,导入cxf依赖的jar包。


2. 服务接口:
package com.huey.dream.ws;

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

/**
 * Web服务接口,注解@WebService说明此接口是一个Web Service
 * @author  huey2672
 * @version 1.0
 * @created 2014-8-12
 */
@WebService
public interface HelloService {

	/**
	 * WebService方法
	 * @param name 注解@WebParam说明参数名称,默认为arg0
	 * @return
	 */
	public String sayHello(@WebParam(name = "name") String name);
	
}

3. 服务实现:
package com.huey.dream.ws.impl;

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

import com.huey.dream.ws.HelloService;

/**
 * WebService的实现
 * @author  huey2672
 * @version 1.0
 * @created 2014-8-12
 */
@WebService(name = "HelloService")
public class HelloServiceImpl implements HelloService {

	public String sayHello(@WebParam(name = "name") String name) {
		return "Hello, " + name + "!";
	}

}

4. 服务端spring配置文件applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xsi:schemaLocation="  
 http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans.xsd   
 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
 
 	<!-- 配置endpoint,指定服务实现类和地址 -->
    <jaxws:endpoint id="HelloServiceImpl"  
        implementor="com.huey.dream.ws.impl.HelloServiceImpl"   
        address="http://localhost:9080/HelloService" />
        
</beans>

5. 发布服务:
package com.huey.dream.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 服务端,发布Web服务
 * @author  huey2672
 * @version 1.0
 * @created 2014-8-12
 */
public class Server {
	
	public Server() {
		System.out.println("Starting Server");
		// 加载spring配置文件后,由spring发布Web服务
		new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	public static void main(String[] args) {
		try {
			new Server();
			System.out.println("Server Ready...");
			Thread.sleep(5 * 60 * 1000);
			System.out.println("Server Existing...");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("ending...");
		}
		
	}
	
}

6. 客户端spring配置文件applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xsi:schemaLocation="  
 http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans.xsd   
 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
    
    <!-- 配置client -->
    <jaxws:client id="client"  
        serviceClass="com.huey.dream.ws.HelloService"   
        address="http://localhost:9080/HelloService" /> 
     
</beans>

7. 调用服务:
package com.huey.dream.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.huey.dream.ws.HelloService;

/**
 * 客户端,调用Web服务
 * @author  huey2672
 * @version 1.0
 * @created 2014-8-12
 */
public class CXFClient {

	public static void main(String[] args) {
		try {
			System.out.println("Starting Client...");
		
			ClassPathXmlApplicationContext context = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
			
			HelloService helloService = (HelloService)context.getBean("client");
			System.out.println(helloService.sayHello("Jin"));
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("ending...");
		}
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值