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 @   expworld  阅读(221)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示