webService之Apache CXF

webService之Apache CXF

标签:webService CXF


一、CXF简介:

    1、Apache CXF是一个开源的webService框架
    2、支持多种协议:支持SOAP1.1,SOAP1.2,RESTFUL HTTP,WS-I BasicProfie,WS-Security,等
    3、CXF大大简化了WebService的创建且与Spring进行无缝集成。

二、CXF的编程思路

    1、编写SEI(服务接口)
    2、编写接口实现类
    3、配置spring配置文件
    4、配置web.xml
    5、启动服务器

三、服务端设计

  1. 服务接口

    /**
     * 天气服务接口
     * @author MOTUI
     *
     */
    @WebService
    public interface WeatherService {
    
        /**
         * 根据城市名称获得天气情况
         * @param cityName
         * @return
         */
        public String getWeather(String cityName);
    }     
  2. 实现类

    /**
     * 服务接口实现类
     * @author MOTUI
     *
     */
    @WebService
    public class WeatherServiceImpl implements WeatherService {
    
        public String getWeather(String cityName) {
            if ("北京".equals(cityName)) {
                return "阴转多云,18~25摄氏度";
            }
            return "其他地区正在完善";
        }
    }
  3. 配置文件(spring 配置文件)
    导入命名空间:

            xmlns:jaxws="http://cxf.apache.org/jaxws"
            xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    
            xsi:schemaLocation="http://cxf.apache.org/jaxws
                                http://cxf.apache.org/schemas/jaxws.xsd
                                http://cxf.apache.org/jaxrs
                                http://cxf.apache.org/schemas/jaxrs.xsd"
    ```xml
    <!-- 配置bean -->         
    <bean id="weatherService" class="com.motui.service.WeatherServiceImpl"/>
    
    <!-- 配置webService -->
    <jaxws:server address="/ws" serviceClass="com.motui.service.WeatherService">
    <jaxws:serviceBean>
        <ref bean="weatherService"/>
    </jaxws:serviceBean>
    </jaxws:server>
    ```
    
  4. web.xml

        <!-- 加载Spring的工厂监听器 -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- 指定监听器寻找Spring配置文件所在位置 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
      </context-param>
    
      <!-- 配置CXF Servlet -->
      <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/cxf/*</url-pattern>
      </servlet-mapping>
  5. 启动服务

    部署到Tomcat,启动服务。
    访问路径:http://localhost:port/项目名称/cxf/ws?wsdl进行访问。
    页面显示wsdl说明书及部署正确。
    

四、客户端设计

  1. 生成客户端代码

    使用命令:wsimport -s . -p 包名 wsdl地址
    
  2. 客户端代码(项目为web项目和java项目都可以):
    可以直接调用也可以在客户端业务层调用,此处在业务层调用

    接口代码:
    
    public interface ClientService {
    
    public String queryWeather(String cityName);
    
    }
    实现类代码:(实现类上添加@service注解)
    
    @Service
    public class ClientServiceImpl implements ClientService {
    
        private WeatherService weatherService;
        public void setWeatherService(WeatherService weatherService) {
            this.weatherService = weatherService;
        }
    
        @Override
        public String queryWeather(String cityName) {
    
            String weather = weatherService.getWeather(cityName);
    
            return weather;
        }
    
    }
    测试类代码:
    
    /**
     * 测试客户端
     */
    public class TestClient {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
            //获得ClientService
            ClientService clientService = (ClientService) context.getBean("clientService");
            //调用方法
            String queryWeather = clientService.queryWeather("北京");
            System.out.println(queryWeather);
        }
    
    }

    测试结果:

    阴转多云,18~25摄氏度
    

    五、总结

    基于CXF与Spring的webService开发。CXF框架与spring可以进行无缝连接。
    在使用CXF时,需要导入CXF的jar包,在导入全部jar在开发中可能会遇到如下问题:
    1、配置文件中不自动提示和报红叉问题。
        解决方式:联网就可以解决。
    2、运行过程中报错:prefix wsdp is not bound to a namespace
        由以下四个jar导致,删除即可,不删除也不影响正常运行。
        cxf-services-ws-discovery-api-3.1.4.jar
        cxf-services-ws-discovery-service-3.1.4.jar
        cxf-services-wsn-api-3.1.4.jar
        cxf-services-wsn-core-3.1.4.jar
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值