1. 程式人生 > >Spring 整合CXF需要的配置及處理步驟

Spring 整合CXF需要的配置及處理步驟

CXF配置
1.web.xml
配置
<!--Spring MVC是通過DispatcherServlet來載入Spring配置檔案的,因此不需要在web.xml中配置ContextLoaderListener。
  但是CXF卻需要通過ContextLoaderListener來載入Spring。-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置CXF框架的核心Servlet  -->
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/server/*</url-pattern>
  </servlet-mapping>
  
2.spring-webservice.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://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-lazy-init="true">

    <!-- 引入CXF Bean定義如下,早期的版本中使用 -->
    <!--<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" />-->
    <!--釋出webservice-->
    <!-- WebService的實現Bean定義 -->
    <!--web.xml配置了webservice的訪問路徑/server/*,那麼/server/web-publish?wsdl就是該webservice的訪問路徑-->
    <bean id="webserviceServer" class="com.lida.dream_webservice.server.MyWebserviceImpl" />
    <!-- jax-ws endpoint定義  -->
    <jaxws:endpoint id="myService" implementor="#webserviceServer"  address="/web-publish" >
    </jaxws:endpoint>
    <!--釋出webservice-->
</beans>

3.寫一個介面,實現服務類介面

4.客服端介面呼叫介面地址

5.需要的jar包
<dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.1.8</version>
    </dependency>

    <!--web service 以下都是cxf必備的-->
    <!--org.apache.cxf.transport.servlet.CXFServlet-->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.1.8</version>
    </dependency>
    <!--不加這個包會報錯Unable to locate spring NamespaceHandler for XML schema namespace [http://cxf.apache.org/jaxws]-->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.1.8</version>
    </dependency>
    <!--java實現webservice,不部署到tomcat,需要jetty包支援-->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>3.1.8</version>
    </dependency>