使用cxf 开发webService 初体验

最近项目开发完毕,目前有闲于时间,所以抽空学习了一下,使用cxf开发WebService,废话不多说,直接上码!

背景介绍:

1.开发工具:eclipse

2.jar:cxf+spring,(cxf的包下集成了spring的包,很方便)

步骤如下:

1.创建web项目

2.引入jar,具体引入请查看图片

3.编写对外接口:

@WebService
public interface ISayHello {
	
	public String sayHello(@WebParam(name="name")String name);
}

 4.编写接口实现:

@WebService(endpointInterface="com.dao.ISayHello")
public class SayHelloImpl implements ISayHello {

	public String sayHello(String name) {
		// TODO Auto-generated method stub
		return "my name is"+name;
	}

}

 5.配置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">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<bean id="sayHelloImpl" class="com.dao.impl.SayHelloImpl" />
	<jaxws:endpoint id="sayHello" implementor="#sayHelloImpl"
		address="/sayHello" />
</beans> 

6.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>webServiceDemo</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
    	 /WEB-INF/config/applicationContext.xml
    </param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

自此使用csf 开发webService 已经完毕。

下面我们使用spring注入的方式,编写一个简单客户端,模拟访问我们刚写的webservice.

步骤如下:

1.编写applicationContext_client.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">
	
	
	<!-- 该方式访问ws服务也是利用spring的依赖注入法 id是spring IOC容器唯一标识符 serviceClass是webservices服务接口 
		address是服务wsdl地址 -->
	<jaxws:client id="sayHello2" serviceClass="com.dao.ISayHello"
		address="http://localhost:8080/webServiceDemo/sayHello?wsdl" />
</beans> 

 2.client:

public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "com/client/applicationContext_client.xml" });

		ISayHello client = (ISayHello) context.getBean("sayHello2");

		String response = client.sayHello("Joe");
		System.out.println("Response: " + response);
		System.exit(0);
	}

 3.运行即可,当然首先要启动 我们刚写的webservice服务,不然会提示连接失败!

猜你喜欢

转载自lgclove1314.iteye.com/blog/2326217