WebService系列博客{十}[CXF简单案例实现]

概述:

CXF是Apache的一个Web Service框架。搭配Jax-Ws使用将部署好的web service发布到tomcat容器中


简单案例:

1、 首先准备好Jar包。可以到apache的官网下载

2、 将下载好的Jar包一次性导入到项目目录[WEB-INF/lib]文件夹下面

3、 解压lib里面的cxf.jar文件,将解压目录下面的[META-INF/cxf]copy到项目的META-INF下面

4、 编写web service接口

@WebService(name = "CxfTester", targetNamespace = "http://org.cxf.com/")
public interface CxfTester {
    public String sendMessage(String sendTo,String message);
}

5、  编写服务接口实现类

@WebService(endpointInterface = "com.cxf.org.CxfTester")
public class CxfTesterImpl implements CxfTester {
	@Override
	public String sendMessage(String sendTo, String Message) {
		String str = "发送到:" + sendTo + "信息详情:" + Message;
		return str;
	}
}

6、  在WEB-INF/建立一个文件夹名为classes。并且新建一个xml文件、名为application

Context.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">
       
<!-- 导入cxf.jar解压出来的文件 -->        
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<!-- 声明Endpoint, 
address 为访问地址的部分地址
implementor   为实现类
 -->
<jaxws:endpoint id="CxFTester" 
	address="/CxfTester" 
	implementor="com.cxf.org.CxfTesterImpl"/>



	
<!-- 指定客户端访问相关情况
class   服务端接口
factory-bean    引用下面声明的类
factory-method   客户端生成调用对象所用方法
-->
<bean id="client" 
		class="com.cxf.org.CxfTester"
			factory-bean="clientFactory"
				factory-method="create"></bean>
				

			
<!-- 配置CXF服务代理bean
serviceClass : 服务端接口
address : 访问地址jaxws:endpoint声明的address在项目名的后面,此处的路径和web.xml配置的urlpattern有联系
 -->				
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
	<property name="serviceClass" value="com.cxf.org.CxfTester"></property>
	<property name="address" value="http://192.168.1.104:8080/Apache_CXF_1/CxfTester"></property>
</bean>

7、  编辑web.xml,设置容器启动加载applicationContext.xml文件。并且配置CXFServlet

<?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" 
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	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>Apache_CXF</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/classes/applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置监听 -->
  <listener>
  	<listener-class>
  		org.springframework.web.context.ContextLoaderListener
  	</listener-class>
  </listener>
  
  <!-- 部署servlet -->
  <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>

8、  发布项目、启动tomcat并且访问wsdl文件

http://localhost:8080/Apache_CXF_1/CxfTester?wsdl


源码下载地址:

http://pan.baidu.com/share/link?shareid=216475&uk=1997312776





你可能感兴趣的:(WebService系列博客{十}[CXF简单案例实现])