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

  • 1.建立maven web工程,使用tomcat发布服务器
    
  • 2.建立坐标,在pom.xml中导入需要依赖的jar包
        完整的pom.xml
        
;; 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"> ;;
    4.0.0
    cn.itcast.maven
    cxf_ws_spring
    0.0.1-SNAPSHOT
    war
    cxf_ws_spring
    
        
            org.apache.cxf
            cxf-rt-frontend-jaxws
            3.0.1
        
        
            org.springframework
            spring-context
            4.1.7.RELEASE
        
        
            org.springframework
            spring-web
            4.1.7.RELEASE
        
        
            org.springframework
            spring-test
            4.1.7.RELEASE
        
        
            junit
            junit
            4.12
        
    
    
        
            
                org.codehaus.mojo
                tomcat-maven-plugin
                1.1
                
                    9800
                
            
        
    
  • 3.配置web.xml文件
完整的web.xml

;;
     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">
     
     
     
          contextConfigLocation
          classpath:applicationContext.xml
     
     
     
          org.springframework.web.context.ContextLoaderListener
     
     
     
          CXFService
          org.apache.cxf.transport.servlet.CXFServlet
          1
     
     
          CXFService
          /services/*
     
     
     
          index.html
          index.htm
          index.jsp
          default.html
          default.htm
          default.jsp
     

  • 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

;;
     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";> ;
     
          
              
          
     
     
配置启动服务的端口:(上面完整的pom.xml中已经配置,如果没有配置,就需要配置下面的代码)
    
        
            
                org.codehaus.mojo
                tomcat-maven-plugin
                1.1
                
                    9800
                
            
        
    

访问 : http://localhost:9998/cxf_ws_spring/services/userService?wsdl
  • 6.整合 spring 测试,编写客户端
在test/resource中建立一个测试的applicationContext-test.xml,编写客户端
;;
     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="cn.itcast.cxf.service.IUserService"
          address=" http://localhost:9800/cxf_ws_spring/services/userService" ;; >
          
          
              
          
          
          
              
          
     
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

你可能感兴趣的:(JAX-WS 和 Spring 整合开发步骤:)