JAX-WS 和 Spring 整合开发步骤:

本文详细介绍了如何将JAX-WS与Spring进行整合,从建立Maven Web工程到配置服务发布,再到Spring测试和编写客户端,提供了一步步的开发步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 1.建立maven web工程,使用tomcat发布服务器
    
  • 2.建立坐标,在pom.xml中导入需要依赖的jar包
        完整的pom.xml
        
    <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"?>
     xmlns=" http://java.sun.com/xml/ns/javaee" ;;
     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 服务发布
           引入名称空间
   

   配置服务:
       完整的appliactionContext.xml

<?xml version="1.0" encoding="UTF-8"?>
     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"?>
     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 配置 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值