以前做项目时做项目时有做过这块内容,今天因为与第三方ERP系统对接需要要在系统中加上webservice的服务端,与调用客户端,中间出了好几处错,现整理下备忘;
先看下我项目的目录结构:
我们的项目使用的是spring+mybatis 采用maven管理;在这基础上加webservice的大致步骤:
1.pom文件添加cxf所需包依赖
<!--CXF配置 data:2015年02月05日 18:04********************************************************-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-common</artifactId>
<version>2.5.4</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.6.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.6.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!--CXF配置 data:2015年02月05日 18:04********************************************************-->
2.web.xml中加入cxf支持
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/com/ningpai/web/config/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--CXF配置 2015-02-06 10:12******************************************-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<!--CXF配置 2015-02-06 10:12 ******************************************-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!--CXF配置 2015-02-06 10:12*******************************************-->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!--CXF配置 2015-02-06 10:12 *******************************************-->
3.在业务模块中编写spring支持xml将webservice服务类用spring进行管理
<?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:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Memcached配置 <import resource="classpath:simplesm-context.xml" /> <aop:aspectj-autoproxy
/> <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
<property name="cacheClientFactory"> <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl"
/> </property> <property name="addressProvider"> <bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="192.168.2.99:11211" /> </bean> </property>
<property name="configuration"> <bean class="com.google.code.ssm.providers.CacheConfiguration">
<property name="consistentHashing" value="true" /> </bean> </property> </bean> -->
<!--CXF配置 2015年02月06日 11:09>>>>>>>>>>>>>>>>>-->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<!--CXF配置 2015年02月06日 11:09<<<<<<<<<<<<<<<<<<-->
<!--CXF Service配置 2015年02月06日 11:27-->
<jaxws:endpoint id="productService" address="/productService" implementorClass="com.kunge.ws.server.IProductService">
<jaxws:implementor>
<bean id="productServiceImpl" class="com.<span style="font-family: Arial, Helvetica, sans-serif;">kunge</span><span style="font-family: Arial, Helvetica, sans-serif;">.ws.server.impl.ProductServiceImpl"></span>
<property name="goodsMapper" ref="GoodsMapper"/>
</bean>
</jaxws:implementor>
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature" />
</jaxws:features>
</jaxws:endpoint>
</beans>
xmlns:jaxws="http://cxf.apache.org/jaxws" <pre name="code" class="html">http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
4.还是web.xml中添加对于自定义xml的引用
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/com/kunge/web/config/ApplicationContext.xml,
<span style="white-space:pre"> </span> classpath*:com/kunge/web/config/ws-servlet.xml
<span style="white-space:pre"> </span></param-value>
</context-param>
这里路径要写对,否则会找不到webservice所写的webservice类,即cxf为定义之类的错误;
这里只有降到相关的配置,具体代码网上有很多实现;本来是想用纯注解的方式写的,因为我dao层service层,controller都是注解,但是试了半天总是显示no services。。还好在cxf的配置中可以引用注解初始化的类;即
<bean id="productServiceImpl" class="com.kunge.ws.server.impl.ProductServiceImpl">
<property name="goodsMapper" ref="GoodsMapper"/>
</bean>