CXF3.*发布WebService服务的方法
spring+cxf的搭建方式
一、在pom.xml添加cxf3.1.14的依赖,JDK1.7
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.14</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.14</version>
</dependency>
二、在web.xml 和spring***.xml文件中配置
???<!-- 配置CXFwebservice的监听器 --> ???<servlet> ???????<servlet-name>CXFServlet</servlet-name> ???????<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> ???????<load-on-startup>4</load-on-startup> ???</servlet> ???<servlet-mapping> ???????<servlet-name>CXFServlet</servlet-name> ???????<url-pattern>/service/*</url-pattern> ???</servlet-mapping>
CXF需要借助ContextLoaderListener监听器
(1)所以我首先在web.xml中引入spring.xml这个核心配置文件
1 <!-- 引入spring.xml文件 -->2 ????<context-param>3 ????????<param-name>contextConfigLocation</param-name>4 ????????<param-value>classpath:spring.xml</param-value>5 ????</context-param>6 ????<listener>7 ????????<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>8 ????</listener>
(2)在spring.xml文件中引入CXF的配置文件spring-cxf-service.xml
1 ????<!-- 引入cxf jar包中的xml配置文件 -->2 ?????<import resource="spring-cxf-service.xml" /> ?
(3)在spring-cxf-service.xml配置文件中加入如下头文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oxm="http://www.springframework.org/schema/oxm" 4 ????xmlns:jaxws="http://cxf.apache.org/jaxws" 5 ????xsi:schemaLocation="http://www.springframework.org/schema/beans ?6 ????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ?7 ????http://www.springframework.org/schema/oxm ?8 ????http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd ?9 ????http://cxf.apache.org/jaxws 10 ????http://cxf.apache.org/schemas/jaxws.xsd">
(4)引入cxf jar包自带的xml文件
1 <!-- cxf 3.*以上只需要引入这一个JAR包自带的xml文件 -->2 ????<import resource="classpath:META-INF/cxf/cxf.xml"/>
到此基本配置完成。
三、首先创建一个接口,类名上加上@WebService注解,方法中加上@WebMethod注解
1 package **.**.*****; 2 ?3 import javax.jws.WebMethod; 4 import javax.jws.WebParam; 5 import javax.jws.WebService; 6 ?7 @WebService 8 public interface IMyWebService { 9 ????@WebMethod10 ????public String firstWebService(String xml) throws Exception;11 12 }
创建接口的实现类
1 package cn.ztesoft.webservice; 2 ?3 import javax.jws.WebService; 4 import org.dom4j.Document; 5 import org.dom4j.DocumentHelper; 6 import org.dom4j.Element; 7 ?8 @WebService(endpointInterface="cn.ztesoft.webservice.IMyWebService") 9 public class MyService implements IMyWebService {10 11 ????@Override12 ????public String firstWebService(String xml) throws Exception {13 ????????System.out.println(xml);14 ????????Element root = null;15 ????????Document document = DocumentHelper.parseText(xml);16 ????????root = document.getRootElement();17 ????????String interfaceCode = root.elementText("posId");18 ????????System.out.println(interfaceCode);19 ????????StringBuffer str=new StringBuffer();20 ????????//拼接报文21 ????????str.append("<status>1</status>");22 ????????str.append("<errorMsg>22</errorMsg>");23 ????????return str.toString();24 ????}25 }
在spring-cxf-service.xml核心配置文件中配置
1 ?<bean id="myService" class="cn.ztesoft.webservice.MyService"></bean>2 ????<!-- implementor注意,引入的参数前面不加#就报错,草 -->3 ????<!-- id是这个接口叫什么名字 implementor是它的实现类 address是范围这个接口服务的地址路径 -->4 ????<jaxws:endpoint id="myServiceWSDL" address="/myService" implementor="#myService"/>
最后部署到tomcat后,就可以在service服务(web.xml中配置)查看发布的WebService接口了
CXF 3.* WebService服务端
原文地址:http://www.cnblogs.com/AlPacino/p/7930078.html