13_CXF和Spring整合發布服務


【服務端】

第一步:建立一個Web項目

第二步:填充CXF jar包

第三步:創建接口及服務類

 

【工程截圖(對比之前的WebService_CXF_Server00)】

 

 

【applicationContext.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

<!-- service -->
<bean id="weatherInterface" class="com.Higgin.ws.service.WeatherInterfaceImpl"></bean>

<!-- 
    發布服務
    使用jaxws:server和jaxws:endpoint可以發布服務
    WebService地址=Tomcat地址值+CXF Servlet的路徑+ /weather 
-->
<jaxws:server address="/weather" serviceClass="com.Higgin.ws.service.WeatherInterface">
    <jaxws:serviceBean>
        <ref bean="weatherInterface"/>
    </jaxws:serviceBean>
</jaxws:server>
</beans>

【web.xml】

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>WebService_CXF_Spring_Server00</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 加載Spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  
  <!-- CXF的Servlet -->
  <servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 本系統的WebService路徑必須以/ws/開頭 -->
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
  
  

</web-app>

【啟動Web容器】

訪問 http://localhost:8080/WebService_CXF_Spring_Server00/ws

 

 接着訪問 http://localhost:8080/WebService_CXF_Spring_Server00/ws/weather?wsdl

可見WebService服務端啟動正常。

 【測試注意】

因為Spring和CXF整合將WebService通過TomCat發布,WebService和應用程序共用一個端口是8080。

測試WebService和應用程序(JSP)是否可以共存(都可以訪問)

正式上線使用80端口。

  

 

【客戶端】

【生成客戶端代碼】

首先,使用利用WebService的wsdl2java工具生成客戶端代碼:

【客戶端工程截圖】

 

【applicationContext.java】

<?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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
    <!-- 
        使用<jaxws:clietn>調用服務端
        jaxws:client內部使用JaxWsProxyFactoryBean方式
        serviceClass:指定portType地址(需要使用wsdl2java工具生成) 
    -->
    <jaxws:client id="weatherClient" address="http://localhost:8080/WebService_CXF_Spring_Server00/ws/weather?wsdl"
        serviceClass="com.higgin.weather.WeatherInterface">
    </jaxws:client>
</beans>

 

【ClientTest.java】

package com.higgin.ws.cxf;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.higgin.weather.WeatherInterface;
import com.higgin.weather.WeatherModel;

public class ClientTest {
    private ApplicationContext applicationContext;
    
    @Before
    public void before(){
        applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    
    @Test
    public void testCxfSpringClient(){
        //從Spring容器中取出portType
        WeatherInterface weatherInterface=(WeatherInterface) applicationContext.getBean("weatherClient");
        
        //調用portType方法
        List<WeatherModel> list=weatherInterface.queryWeather("杭州");
        
        for(WeatherModel weatherModel:list){
            System.out.println(weatherModel.getDetail());
            Date date=weatherModel.getDate().toGregorianCalendar().getTime();
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
            System.out.println(weatherModel.getTemperatureMax());
            System.out.println(weatherModel.getTemperatureMin());
        }
        
    }
}

 

【運行結果】

 

 

【總結:使用jaxws實現SOAP1.1、SOAP1.2】

方式一:

CXF編程實現:

1.使用jaxwsServerFactoryBean發布WebService服務端。

  需要設置:

  jaxwsServerFactoryBean.setAddress("WebService地址");

  jaxwsServerFactoryBean.setServiceClass("porType類路徑"); //由程序員編寫的

  jaxwsServerFactoryBean.setServiceBean("portType類對象");

  jaxwsServerFactoryBean.create();  //發布一個服務

2.使用jaxwsProxyFactory實現客戶端調用WebService服務

  jaxwsServerFactoryBean.setAddress("WebService的wsdl地址");

  jaxwsServerFactoryBean.setServiceClass("portType路徑");  //portType是wsdl2java工具生成

  jaxwsServerFactoryBean.setCreate();   //創建portType對象

 

方法二:

CXF和Spring整合開發服務端和客戶端。

1.使用<jaxws:Server>發布WebService服務端

   在<jaxws:Server>設置Address、serviceClass、serviceBean

2.使用<jaxws:Client>調用WebService服務

 在<jaxws:Server>設置Address、serviceClass


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM