spring與cxf的整合

一、在web.xml中配置web service的servlet
   
  <servlet >
       <servlet-name >cxf-ws</servlet-name >
       <servlet-class >org.apache.cxf.transport.servlet.CXFServlet </servlet-class>
  </servlet >
  <servlet-mapping >
       <servlet-name >cxf-ws</servlet-name >
       <url-pattern >/ ws/*</url-pattern >
  </servlet-mapping >

二、新建一個applicationContext-ws.xml文件

     在web.xml中引入該配置文件

  <context-param>
    <param-name >contextConfigLocation </param-name>
    <param-value >
           classpath:spring/applicationContext-ws.xml
    </param-value>
  </context-param >

  applicationContext-ws.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" 
        xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd    
             http://cxf.apache.org/transports/http/configuration   
             http://cxf.apache.org/schemas/configuration/http-conf.xsd " >

     </beans>

三、在applicationContext-ws.xml引入cxf的配置文件:

    <!-- Import Apache CXF Bean Definition -->
    <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"/>
     <!-- end -->

四、定義一個web service server端

    <!-- 暴露webservice 1 -->
    <jaxws:server id="helloServer"
        serviceClass="com.iit.song.ws.service.IHello"
        address= "/hello">
        <jaxws:serviceBean>
            <ref bean= "hello"/> <!-- 要暴露的 bean 的引用 -->
        </jaxws:serviceBean>
    </jaxws:server >
    <!-- end -->

     wsdl地址爲:127.0.0.1:8080/ws/hello?wsdl

    < bean id ="hello" class ="com.iit.song.ws.service.Hello"></bean >

     1> 定義一個web service接口
   
      @WebService
public interface IHello {
    public String say();
}

2>實現該接口
@WebService
public class Hello  implements IHello{
    @Override
    public String say(){
          return "hello world" ;
    }
}

五、定義一個interceptor:
     
     <!-- 暴露webservice 1 -->
    <jaxws:server id= "helloServer"
        serviceClass ="com.iit.song.ws.service.IHello"
        address= "/hello" >
        < jaxws:serviceBean>
            < ref bean= "hello" /> <!-- 要暴露的 bean 的引用 -->
        </ jaxws:serviceBean>

        < jaxws:inInterceptors >
            < ref bean= "testInInterceptor" />
        </ jaxws:inInterceptors >

    </jaxws:server >
    <!-- end -->

     
     在applicationContext-ws.xml中配置interceptor
     < bean id ="testInInterceptor" class="com.iit.song.ws.interceptor.TestInInterceptor"></bean >

     定義該interceptor:
     public class TestInInterceptor extends AbstractPhaseInterceptor<SoapMessage>{    
    private Logger logger = LoggerFactory.getLogger(TestInInterceptor. class);   
    public TestInInterceptor(String phase){
          super(phase);
    }    
    public TestInInterceptor(){
          super(Phase.PRE_INVOKE );
    }
    @Override
    public void handleMessage(SoapMessage message) throws Fault {
         System. out.println("in interceptor" );
    }
     }

六、定義一個handler:
     <!-- 暴露webservice 1 -->
    <jaxws:server id= "helloServer"
        serviceClass ="com.iit.song.ws.service.IHello"
        address= "/hello" >
        < jaxws:serviceBean>
            < ref bean= "hello" /> <!-- 要暴露的 bean 的引用 -->
        </ jaxws:serviceBean>

        <jaxws:handlers>
               <ref bean= "handler"/>
        </jaxws:handlers>

    </jaxws:server >
    <!-- end -->


     在applicationContext-ws.xml中添加該行:
     < bean id ="handler" class ="com.iit.song.ws.handler.TestHandler"></bean >

     
     public class TestHandler implements SOAPHandler<SOAPMessageContext>{
    @Override
    public boolean handleMessage(SOAPMessageContext messagecontext) {
         SOAPMessage message = messagecontext.getMessage();
          try {
             message.writeTo(System. out);
         } catch (SOAPException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
          return true ;
    }
    @Override
    public boolean handleFault(SOAPMessageContext messagecontext) {
         System. out.println("handleFault" );
         SOAPMessage message = messagecontext.getMessage();
          try {
             message.writeTo(System. out);
         } catch (SOAPException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
          return true ;
    }
    @Override
    public void close(MessageContext messagecontext) {
         
    }
    @Override
    public Set<QName> getHeaders() {
         System. out.println("getheaders" );
          return null ;
    }
}


七、spring 客戶端配置

<jaxws:client id= "hello"
   serviceClass="com.iit.song.ws.service.IHello" 
   address="http://localhost:8080/ws/IHello" >
       <jaxws:outInterceptors>
           <ref bean= "headerInterceptor" />
       </jaxws:outInterceptors>
</jaxws:client>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章