CXF整合Spring入门教程

开发步骤:

1,apache.org下载CXF项目

2,解压cxf后,导入lib下的部分包:cxf、spring相关的包

3,创建接口与接口实现类
<!--接口-->
package com.gufengxiachen.cxf.service;

import  javax.jws.WebParam;
import  javax.jws.WebService;

@WebService
public   interface  MessageService {
    String getMessage(  @WebParam  (name= "text"  ) String text);       
}
<!--实现类-->
package  com.gufengxiachen.cxf.service.impl;

import  javax.jws.WebService;

import  com.gufengxiachen.cxf.service.MessageService;

@WebService  (endpointInterface =  "com.gufengxiachen.cxf.service.MessageService"  ,  
        serviceName =  "MessageService"  
public   class  MessageServiceImpl  implements  MessageService{

      public  String getMessage(String text) {
           //  TODO  Auto-generated method stub
           return   "Welcome"  +text;
    }

}
4,编写web.xml与beans.xml文件如下:
<!--web.xml-->
<?  xml   version  = "1.0"  encoding =  "UTF-8"  ?>
<  web-app   version  = "2.5"
      xmlns  = "http://java.sun.com/xml/ns/javaee"
      xmlns:xsi  = "http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation  = "http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  >
      < context-param  >   
         <  param-name >  contextConfigLocation  </ param-name  >   
         <  param-value >  /WEB-INF/beans.xml  </ param-value  >   
     </ context-param  >   
 
     < listener  >   
         <  listener-class >   
            org.springframework.web.context.ContextLoaderListener
         </  listener-class >   
     </ listener  >   
 
     < servlet  >   
         <  servlet-name >  CXFServlet  </ servlet-name  >   
         <  display-name >  CXF Servlet  </  display-name >   
         <  servlet-class >   
            org.apache.cxf.transport.servlet.CXFServlet 
         </  servlet-class >   
         <  load-on-startup >  1 </  load-on-startup  >  
     </ servlet  >   
 
     < servlet-mapping  >   
         <  servlet-name >  CXFServlet  </ servlet-name  >   
         <  url-pattern >  /services/*  </ url-pattern  >
     </ servlet-mapping  >   
</  web-app >

<!--beans.xml-->

<?  xml   version  = "1.0"  encoding =  "UTF-8"  ?>  
 
<!-- START SNIPPET: beans -->   
<  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/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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"  />  
 
     < jaxws:endpoint    
       id =  "messageService"    
       implementor  = "com.gufengxiachen.cxf.service.impl.MessageServiceImpl"    
       address  = "/MessageService"  />
       
</  beans >    

部署在tomcat下,运行在浏览器输入:

客户端调用代码:
package  com.gufengxiachen.cxf.client;

import  org.apache.cxf.interceptor.LoggingInInterceptor;
import  org.apache.cxf.interceptor.LoggingOutInterceptor;
import  org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import  com.gufengxiachen.cxf.service.MessageService;

public   class  MessageServiceClient {
    
      public   static   void  main(String[] args) {
             JaxWsProxyFactoryBean factory =  new  JaxWsProxyFactoryBean(); 
            factory.getInInterceptors().add(  new  LoggingInInterceptor()); 
            factory.getOutInterceptors().add(  new  LoggingOutInterceptor()); 
            factory.setServiceClass(MessageService.  class ); 
            factory.setAddress( "http://localhost:8080/cxf/services/MessageService"  ); 
            MessageService client = (MessageService) factory.create(); 
     
            String reply = client.getMessage(  "张三"  ); 
            System.  out  .println( "Server said: "  + reply); 
    }

}

附录:使用wsdl2java工具生成客户端代码:
cmd命令在cxf/bin目录下,运行:
wsdl2java -p 包名 -d 目录 wsdl url
例如:
wsdl2java -p com.gufengxiachen.cxf.service -d D:/cxf/client http://127.0.0.1/cxf/services/MessageService?wsdl



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值