• 分布式.RPC-WebService CXF框架


     系列博文:

    分布式.RPC调用-RMI & Hessian框架_闲猫的博客-CSDN博客

    分布式.RPC-WebService三要素,三个规范, Soap协议_闲猫的博客-CSDN博客

    分布式.RPC-WebService入门案例(java实现,注解实现,xsd文件解析,wsdl文件解析)_闲猫的博客-CSDN博客

    分布式.RPC调用-RMI & Hessian框架_闲猫的博客-CSDN博客

    分布式.RPC-WebService CXF框架_闲猫的博客-CSDN博客

    分布式.RPC-WebService Restful风格实现_闲猫的博客-CSDN博客


    目录

    CXF简介

    入门案例

    服务器

    客户端

    CXF开发SOAP1.2

    CXF和Spring整合

    服务器

    客户端


    CXF简介

           CXF是Apache公司产品,可以使用轻松实现WebService功能。

    入门案例

    服务器

    步骤:

    1. 创建java工程,引入cxf的jar包
    2. 编写服务类
    3. 发布服务

    代码:

    JaxWsServerFactoryBean server [ww1] = new JaxWsServerFactoryBean();

    server.setAddress[ww2] ("http://localhost:12345/member");

    server.setServiceBean[ww3] (new MemberService());

    server.create[ww4] ();


     [ww1]创建JaxWsdl的工厂Bean

     [ww2]设置地址

     [ww3]设置获取的服务器类

     [ww4]发布服务

    测试:

    客户端

    步骤:

    1. 创建java工程 ,引入cxf的jar包

    2. 生成本地代码

             

      3. 调用远程服务

    代码:

    JaxWsProxyFactoryBean client = new JaxWsProxyFactoryBean();

    client.setAddress("http://localhost:12345/member");

    client.setServiceClass(MemberService.class);

    MemberService memberService = (MemberService) client.create()[ww1] ;

    Member member = memberService.getMember("");[ww2] 

    System.out.println(member.getName());


     [ww1]获取接口,这时候MemberService是接口(服务器中是类)

     [ww2]调用,就跟调用自己的函数一样的格式

    CXF开发SOAP1.2

           只需加注解@BindingType(SOAPBinding.SOAP12HTTP_BINDING),不需要引入扩展包。

    CXF和Spring整合

           这里发布两个模块服务。

    服务器

    步骤:(示例的配置只看格式,不看内容)

    1. 创建web工程,添加cxf的jar包 和applicationContext.xml

    2. 修改web.xml  添加spring监听

            添加spring的监听器,并且配置配置文件扫描路径

            

     3. 修改web.xml  添加CXFServlet控制器

            

     4. 编写服务类

    5. 编写applicationContext.xml

            单个服务接口发布:

            

            多个服务接口发布:

             

    6. 测试

             

     代码:

    1. 创建web工程,添加cxf的jar包 和applicationContext.xml

            工程:

             

    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"

    [ww1]      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

    [ww2]                                       http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

    beans>


     [ww1]引入WebService的命名空间

     [ww2]引入WebService的命名空间

    2. 修改web.xml  添加spring监听

            Spring监听器名称:org.springframework.web.context.ContextLoaderListener

            将Spring配置文件配置在全局参数中:contextConfigLocation

    <context-param>

        <param-name>contextConfigLocationparam-name>

        <param-value>classpath:applicationContext.xmlparam-value>

    context-param>

    <listener>

        <listener-class>

               org.springframework.web.context.ContextLoaderListener

         listener-class>

    listener>

    3. 修改web.xml  添加CXFServlet控制器

            CxfServlet控制器:org.apache.cxf.transport.servlet.CXFServlet

    <servlet>

        <servlet-name>cxfServletservlet-name>

        <servlet-class>

             org.apache.cxf.transport.servlet.CXFServlet

        servlet-class>

    servlet>

    <servlet-mapping>

        <servlet-name>cxfServletservlet-name>

        <url-pattern>/ws/*url-pattern>     

    servlet-mapping>

    4. 编写服务类

            WeatherService服务类:(添加了@WebService注解的普通类)

    @We        bService

    public class WeatherService {

       public Weather getWeather(String str) {

          return new Weather(123l, "赵菲");

       }

    }

    MemberService这个服务类就不列了,一样。

    5. 编写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">

    <bean id="member"

           class="cn.itcast.Service.member.MemberService" />

    <bean id="weather"

           class="cn.itcast.Service.weather.WeatherService"/>

      

    <jaxws:server address="/member[ww1] ">

        <jaxws:serviceBean>

          

           <ref bean="member"/>

        jaxws:serviceBean>

    jaxws:server>

    <jaxws:server address="/weather">

        <jaxws:serviceBean>

           <ref bean="weather"/>

        jaxws:serviceBean>

    jaxws:server>

       

    beans>


     [ww1]需要添加到servlet后边的

    6. 测试

            

    测试地址:

             http://localhost:8080/server/ws/member?wsdl

            http://localhost:8080/server/ws/weather?wsdl

    地址组成:

            server:项目名称

            ws:servlet需要匹配的那个地址

            

            member和 weather 配置的访问路径: 

            

            ?wsdl 这个是访问wsdl的约定 

    附录:完整web.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app version="2.5"
    3. xmlns="http://java.sun.com/xml/ns/javaee"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    7. <context-param>
    8. <param-name>contextConfigLocation</param-name>
    9. <param-value>
    10. classpath:applicationContext.xml
    11. </param-value>
    12. </context-param>
    13. <listener>
    14. <listener-class>
    15. org.springframework.web.context.ContextLoaderListener
    16. </listener-class>
    17. </listener>
    18. <servlet>
    19. <servlet-name>cxfServlet</servlet-name>
    20. <servlet-class>
    21. org.apache.cxf.transport.servlet.CXFServlet
    22. </servlet-class>
    23. </servlet>
    24. <servlet-mapping>
    25. <servlet-name>cxfServlet</servlet-name>
    26. <url-pattern>/ws/*</url-pattern>
    27. </servlet-mapping>
    28. <welcome-file-list>
    29. <welcome-file>index.jsp</welcome-file>
    30. </welcome-file-list>
    31. </web-app>

    客户端

    步骤:

    1. 创建java工程 ,添加cxf的jar包 和applicationContext.xml

    2. 生成本地代码

    3. 编写applicationContext.xml 

            

     4. 编写代码调用本地代理

            

    代码:

    1. 创建java工程 ,添加cxf的jar包 和applicationContext.xml

             

    2. 生成本地代码

            将cmd的当前地址导在工程下的src目录,使用:wsimport –s . 地址 ,两次生成客户端代码。

            wsimport –s . http://localhost:8080/server/ws/member?wsdl

            wsimport –s . http://localhost:8080/server/ws/weather?wsdl

    3. 编写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">

        <jaxws:client

            id="member[ww1] " 

            address="http://localhost:8080/server/ws/member?wsdl[ww2] "

             serviceClass="cn.itcast.service.member.MemberService[ww3] "/>

               

        <jaxws:client

            id="weather"

            address="http://localhost:8080/server/ws/weather?wsdl"

            serviceClass="cn.itcast.service.weather.WeatherService"/>

       

    beans>   


     [ww1]这个是在Main中调用是使用

     [ww2]测试是访问的地址

     [ww3]生成的客户端服务类,是一个接口

    4. 编写代码调用本地代理

    public static void main(String[] args) {

        ApplicationContext context [ww1] = new ClassPathXmlApplicationContext(

                "applicationContext.xml");

        WeatherService weatherService [ww2] = (WeatherService) context

                .getBean("weather");

        Weather weather = weatherService.getWeather("");[ww3] 

        System.out.println(weather.getName());

        MemberService memberService = (MemberService) context.getBean("member");

        Member member = memberService.getMember("");

        System.out.println(member.getName());

    }


     [ww1]获取ApplicationContext对象

     [ww2]获取服务对象,这个weather就是上面配置的jaxws的id

     [ww3]使用该接口进行调用即可


    END

  • 相关阅读:
    大数据安全概述
    DeFi还没搞明白,DeSci又来了?
    MybatisPlus
    单例模式4种实现方式C++
    题目 1282: 公交汽车
    【Visual Leak Detector】配置项 ForceIncludeModules
    路由懒加载
    【2018统考真题】给定一个含n(n≥1)个整数的数组,请设计一个在时间上尽可能高 效的算法,找出数组中未出现的最小正整数。
    pnpm、npm、yarn 包管理工具『优劣对比』及『环境迁移』
    linux线程互斥锁
  • 原文地址:https://blog.csdn.net/weixin_42754896/article/details/126448169