1. 新建工程,导入cxf依赖的jar包。
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...");
}
}
}