利用cxf3.14+spring4.2發佈webservice

1 開發工具
IDEA
2 maven 依賴 pom.xml

 <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-frontend-jaxws</artifactId>
          <version>3.1.4</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty -->
      <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-transports-http-jetty</artifactId>
          <version>3.1.4</version>
      </dependency>
       <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>

        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-expression</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc-portlet</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-websocket</artifactId>
          <version>4.2.0.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>RELEASE</version>
      </dependency>

3 web.xml相關配置

<!-- 配置spring資源路徑 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:SpringMVCServlet-servlet.xml</param-value>
  </context-param>
  <!--配置spring上下文監聽器  -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 字符集過濾 -->
  <filter>
    <description>字符集過濾器</description>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <init-param>
      <description>字符集編碼</description>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
 <servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <!-- 配置cxf的訪問地址 -->
  <!-- 本系統中webService必須以/ws/*開頭 -->
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>

4 spring的相關配置文件 SpringMVCServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation=" 
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-4.2.xsd 
                    http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
                    http://www.springframework.org/schema/mvc 
                    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
                    http://www.springframework.org/schema/aop 
                    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

                    http://cxf.apache.org/core
                    http://cxf.apache.org/schemas/core.xsd
                    http://cxf.apache.org/bindings/soap 
                    http://cxf.apache.org/schemas/configuration/soap.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"

xmlns:p="http://www.springframework.org/schema/p" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:soap="http://cxf.apache.org/bindings/soap">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    
    <context:component-scan base-package="com.wyn.app;com.ws.weather"></context:component-scan>

    <!--配置cxf webservice服務-->
    <!--配置發佈服務端-->
    <bean id="weatherInterface" class="com.wyn.app.serviceImp.WeatherInterfaceImpl"></bean>

    <!--
        發佈服務
        和使用endpoint發佈服務類似
        WebService地址=tomcat地址+cxf+/weather
     -->
    <jaxws:server address="/weather" serviceClass="com.wyn.app.service.WeatherInterface">
        <jaxws:serviceBean>
            <ref bean="weatherInterface" />
        </jaxws:serviceBean>
    </jaxws:server>


    <!-- 使用client標籤調用服務端 -->
    <jaxws:client
            id="weatherClient"
            address="http://localhost:8080/ws/weather?wsdl"
            serviceClass="com.ws.weather.WeatherInterface">

    </jaxws:client>

    <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前後綴 -->
    <!-- <bean p:suffix=".jsp" p:prefix="/" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> -->
    <!--配置ModelAndView(jsp)解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
        <property name="prefix" value="/"></property>  
        <property name="suffix" value=".jsp"></property><!--可爲空,方便實現自已的依據擴展名來選擇視圖解釋類的邏輯  -->
    </bean>
    <!-- 靜態資源的訪問方式 -->
    <mvc:default-servlet-handler/>
</beans>

5 實體類 weatherModel

package com.wyn.app.entity;

import java.util.Date;

/**
 * Created by wyn on 2017/8/23.
 */
public class WeatherModel {
    //天氣概況
    private String detail;

    //日期
    private Date data;

    //最高溫度
    private int temperature_max;

    //最低溫度
    private int temperature_min;

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public Date getData() {
        return data;
    }

    public void setData(Date data) {
        this.data = data;
    }

    public int getTemperature_max() {
        return temperature_max;
    }

    public void setTemperature_max(int temperature_max) {
        this.temperature_max = temperature_max;
    }

    public int getTemperature_min() {
        return temperature_min;
    }

    public void setTemperature_min(int temperature_min) {
        this.temperature_min = temperature_min;
    }

    @Override
    public String toString() {
        return "WeatherModel{" +
                "detail='" + detail + '\'' +
                ", data=" + data +
                ", temperature_max=" + temperature_max +
                ", temperature_min=" + temperature_min +
                '}';
    }
}

6 service 接口層

package com.wyn.app.service;

import com.wyn.app.entity.WeatherModel;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import java.util.List;

/**
 * Created by wangyanan on 2017/8/24.
 */
@WebService(
        targetNamespace="http://weather.ws.com/",//指定 wsdl的命名空間
        name="WeatherInterface",//指定portType的名稱
        portName="WeatherInterfacePort",//指定port的名稱
        serviceName="WeatherService"//服務視圖的名稱
)
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
    public @WebResult(name="list")
    List<WeatherModel> queryWeather(@WebParam(name="cityName")String name);
}

7 serviceImp 接口實現類層

package com.wyn.app.serviceImp;

import com.wyn.app.entity.WeatherModel;
import com.wyn.app.service.WeatherInterface;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wyn on 2017/8/24.
 */
public class WeatherInterfaceImpl implements WeatherInterface{

    public List<WeatherModel> queryWeather(String name) {
        //構造測試數據
        List<WeatherModel> list = new ArrayList<WeatherModel>();

        WeatherModel weatherModel_1  =new WeatherModel();
        weatherModel_1.setDetail("晴");
        weatherModel_1.setData(new Date());
        weatherModel_1.setTemperature_max(30);
        weatherModel_1.setTemperature_min(28);

        WeatherModel weatherModel_2  =new WeatherModel();
        weatherModel_2.setDetail("晴轉多雲");
        weatherModel_2.setData(new Date());
        weatherModel_2.setTemperature_max(24);
        weatherModel_2.setTemperature_min(20);

        list.add(weatherModel_1);
        list.add(weatherModel_2);
        return list;
    }
}

8 接口訪問測試
這裏寫圖片描述

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