JAX-WS 和 Spring 整合开发步骤:
2017-12-13 09:07
253 查看
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"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";;
xmlns="http://java.sun.com/xml/ns/javaee";;
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"?>
<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd";>;
<!-- 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>
访问 :http://localhost:9998/cxf_ws_spring/services/userService?wsdl
6.整合 spring 测试,编写客户端
在test/resource中建立一个测试的applicationContext-test.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd";>;
<!--
serviceClass 服务接口
address 服务访问地址
-->
<jaxws:client id="userServiceClient"
serviceClass="cn.itcast.cxf.service.IUserService"
address="http://localhost:9800/cxf_ws_spring/services/userService";;
>
<!-- 来源消息拦截器 -->
<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 配置
例如:http://localhost:9800/cxf_ws_spring/services/userService
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"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";;
xmlns="http://java.sun.com/xml/ns/javaee";;
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"?>
<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd";>;
<!-- 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>
访问 :http://localhost:9998/cxf_ws_spring/services/userService?wsdl
6.整合 spring 测试,编写客户端
在test/resource中建立一个测试的applicationContext-test.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd";>;
<!--
serviceClass 服务接口
address 服务访问地址
-->
<jaxws:client id="userServiceClient"
serviceClass="cn.itcast.cxf.service.IUserService"
address="http://localhost:9800/cxf_ws_spring/services/userService";;
>
<!-- 来源消息拦截器 -->
<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 配置
例如:http://localhost:9800/cxf_ws_spring/services/userService
相关文章推荐
- 构建轻量级webservice-Spring整合jaxws开发
- JAX-WS + Spring 开发webservice
- Spring与Metro(jaxws)整合
- WebService学习之旅(三)JAX-WS与Spring整合发布WebService
- JAX-RS 和 和 Spring 整合开发
- CXF整合Spring之JaxWsProxyFactoryBean调用
- Spring+SpringMVC的整合 具体的开发步骤和xml文件配置
- Spring整合JAX-WS
- WebService学习之旅(三)JAX-WS与Spring整合发布WebService
- WebService学习之旅(三)JAX-WS与Spring整合发布WebService
- Hibernate+Spring整合开发步骤
- 框架 day64 WebService(注解),CXF框架(jax-ws,Jax-rs,与spring整合)
- Java开发Web Service客户端技巧:wsimport、jaxws-maven-plugin、整合Spring
- Eclipse使用Axis2,spring开发jax-ws(webservice)的配置讲解
- Spring+Hibernate+Strust 整合开发步骤
- Struts + Spring +ibatis 整合开发步骤
- JAX-WS整合Spring
- Spring整合JAX-WS
- Struts + Spring +ibatis 整合开发步骤
- Spring Boot整合spring-ws开发web service