1.首先在pom.xml里添加cxf的依赖,如下:
<!-- webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>2.2.3</version>
<type>pom</type>
</dependency>
2.配置web.xml,如下:
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
3.编写webservice与spring整合的配置文件ws.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Repository and Service layers
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<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"/>
<!-- ========================= RESOURCE DEFINITIONS ========================= -->
<!-- <bean id="myservice" class="cn.com.mofit.mofeni.ts.services.MyNewServiceImpl"/>
<jaxws:endpoint id="runMethod" implementor="#myservice"
address="/newService" /> -->
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:server id="aaa" serviceClass="cn.com.mofit.mofeni.ts.services.MyNewService" address="/aaa">
<jaxws:outInterceptors>
<ref bean="outLoggingInterceptor"/>
</jaxws:outInterceptors>
</jaxws:server>
</beans>
4.然后在web.xml里加载ws.xml,如果sping里使用了注解自动扫描bean,那么在web.xml里不能使用(更正:可以使用这种方式,因为我的web.xml已经配置了context-param,只能配置一个)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ws.xml</param-value>
</context-param>
来配置ws.xml,需要如下配置
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml,/WEB-INF/ws.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
否则会找不到sping自动加载的bean
5.编写webservice接口
package cn.com.mofit.mofeni.ts.services;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface MyNewService {
String runMethod(String syncId) ;
}
6编写webservice实现类
package cn.com.mofit.mofeni.ts.services;
import javax.jws.WebService;
import javax.jws.WebParam;
@WebService(endpointInterface="cn.com.mofit.mofeni.ts.services.MyNewService")
public class MyNewServiceImpl implements MyNewService{
public String runMethod(String syncId) {
return "hello "+syncId;
}
}
7,maven clean,maven install,部署工程到tomcat,保持均不报错后,在浏览器里输入http://localhost:8081/services_manager/ws/aaa?wsdl
出现如下界面成功: