11.web-cxf-spring的整合发布webservice服务【完整代码】

 

 

 

第一步:JAR包引入

第二步:码代码:

 

06.web-cxf-spring\src  \ 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">


    <!--通过spring发布webservice服务-->
    <!-- 1.创建 天气服务对象-->
    <bean id="weatherService" class="com.gyf.service.WeatherInterfaceImpl"></bean>

    <!-- 2.发布服务-->
    <jaxws:server address="/weather" serviceClass="com.gyf.service.WeatherInterface">
        <jaxws:serviceBean>
            <ref bean="weatherService"></ref>
        </jaxws:serviceBean>
    </jaxws:server>

    <jaxws:endpoint address="/hello" implementor="com.gyf.service.HelloInterfaceImpl"></jaxws:endpoint>
</beans>

 

 

 

06.web-cxf-spring\web\WEB-INF \ web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- 1.加载spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--2.配置cxf servlet-->
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    
    <!--3.配置cxf的servet映射路径-->
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
</web-app>

 

WeatherInterface .java

package com.exp.service;

import javax.jws.WebService;

@WebService
public interface WeatherInterface {

    /**
     * 根据城市获取天气信息
     * @param cityName
     * @return
     */
    public String queryWeather(String cityName);

    /**
     * 根据省份获取城市
     * @param provinceName
     * @return
     */
    public String[] getCityNameByProvince(String provinceName);
}

 

WeatherInterfaceImpl.java

package com.exp.service;

public class WeatherInterfaceImpl implements WeatherInterface{

    /**
     * 根据城市获取天气信息
     * @param cityName
     * @return
     */
    public String queryWeather(String cityName){

        if("广州".equals(cityName)){
            return cityName +": 天气晴,白天最高温度30度,未天三天都晴";
        }else{
            return cityName +": 天气雨,白天最高温度30度,未天三天都雨";
        }
    }


    /**
     * 根据省份获取城市
     * @param provinceName
     * @return
     */
    public String[] getCityNameByProvince(String provinceName){
        if("广东".equals(provinceName)){
            return new String[]{"广州","深圳","珠海","东莞"};
        }else{
            return new String[]{"未开通"};
        }
    }
}

 

posted @ 2019-05-01 13:55  expworld  阅读(228)  评论(0)    收藏  举报