- 1.建立maven web工程,使用tomcat发布服务器
- 2.建立坐标,在pom.xml中导入需要依赖的jar包
完整的pom.xml
<project xmlns="
http://maven.apache.org/POM/4.0.0"
;; xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
;;
xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
;;
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast.maven</groupId>
<artifactId>cxf_ws_spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>cxf_ws_spring</name>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9800</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 3.配置web.xml文件
完整的web.xml
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
;;
id="WebApp_ID" version="2.5">
<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- cxf基于web访问 -->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<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>
</web-app>
- 4. 导入实体类、Service
- 5.配置 spring cxf 服务发布
引入名称空间xmlns:jaxws=" http://cxf.apache.org/jaxws" ;; http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd
配置服务:
完整的appliactionContext.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
;; xmlns:jaxws="
http://cxf.apache.org/jaxws"
;;
xsi:schemaLocation="
<!-- address:客户端访问服务的路径
serviceClass:配置接口
serviceBean:配置实现类
-->
<jaxws:server id="userService" address="/userService" serviceClass="cn.itcast.cxf.service.IUserService">
<jaxws:serviceBean>
<bean class="cn.itcast.cxf.service.UserServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
配置启动服务的端口:(上面完整的pom.xml中已经配置,如果没有配置,就需要配置下面的代码)
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9800</port>
</configuration>
</plugin>
</plugins>
</build>
- 6.整合 spring 测试,编写客户端
在test/resource中建立一个测试的applicationContext-test.xml,编写客户端
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
;; xmlns:jaxws="
http://cxf.apache.org/jaxws"
;;
xsi:schemaLocation="
<!--
serviceClass 服务接口
address 服务访问地址
-->
<jaxws:client id="userServiceClient"
serviceClass="cn.itcast.cxf.service.IUserService"
<!-- 来源消息拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
</jaxws:inInterceptors>
<!-- 输出消息拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
</beans>
7.如果需要测试,测试用例编写
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= "classpath:applicationContext-test.xml")
public class cxf_ws_springTest {
@Autowired
private IUserService p;
@Test
public void test(){
System.out.println(p.sayHello("黑马程序员"));
}
}
注意:访问的路径:
服务器根目录地址 + web.xml 配置(url ) + applicationContext.xml address 配置