zoukankan      html  css  js  c++  java
  • 远程调用服务框架-CXF(WebServic)

    介绍

    • 远程调用web服务,不需要自己编写具体代码,只需要调用作者给出的接口即可.

    • 我们可以调用互联网上查询天气信息Web服务,然后将它嵌入到我们的程序(C/S或B/S程序)当中来,当用户从我们的网点看到天气信息时,他会认为我们为他提供了很多的信息服务,但其实我们什么也没有做,只是简单调用了一下服务器上的一段代码而已。

    规则

    • 基于http 协议

    • 传输的内容为xml格式

    • SOAP作为一个基于XML语言的协议用于在网上传输数据。

      • SOAP = 在HTTP的基础上+XML数据。
    • WSDL – WebService Description Language – Web服务描述语言。

      • 用来描述服务的地址和基本方法,java程序通过WSDL来生成对应的代理类来调用具体的方法

    Java内置WebService实现

    • 服务端(java 项目),不需要引入jar。

      // 服务端启动服务
      @WebService   // 注解
      public class HelloService {  
          
          public String sayHello(String name){
              System.out.println("服务端say hello");
              return "hello"+name;
          }
          
          public static void main(String[] args) {  // 启动服务
              String address="http://172.27.109.133:8081/hello";   // ip为本机ip  打开cmd  输入ipconfig 找到ipv4即可
              Object implementor=new HelloService();
              Endpoint.publish(address, implementor);
          }
      
      }
      
      • 当服务端启动服务以后,访问http://172.27.109.133:8081/hello?wsdl出现以下内容则表示服务成功启动,如下图:

    • 客户端

      • 首先需要通过wsimport命令解析出对应的Java文件,然后复制到项目目录

        /*
         * 调用服务
         * cmd进入到任意目录 执行命令 wsimport -s . http://192.168.0.108:8080/hello?wsdl
         * 复制文件到项目
         * 然后进行调用 
         * */
        public class app {
            public static void main(String[] args) {
            	// 调用方法
                HelloServiceService helloServiceService = new HelloServiceService();   
                HelloService helloServicePort = helloServiceService.getHelloServicePort();
                String sayHello = helloServicePort.sayHello("笑笑");
                System.out.println(sayHello);
            }
        
        }
        
        
    • 调用成功

    使用CXF框架和Spring整合

    CXF是apache旗下的开源框架,由Celtix + XFire这两门经典的框架合成,是一套非常流行的web service框架。

    注意:CXF2.X版本和Spring4.X不兼容,要用CXF3.X

    服务端

    • 编写接口

      @WebService
      public interface HelloWorldInterface {
          String sayHello(String text);
      }
      
      
    • 编写实现类

    //@WebService在实现类的注解让CXF知道WSDL创建时所使用的接口。
    @WebService(endpointInterface = "webservice.service.HelloWorldInterface")
    public class ServerToJava implements HelloWorldInterface {

    public String sayHello(String text) {
        return "hello" + text;
    }
    

    }

    ```
    
    • 导入项目jar Maven:

      <properties>
      	<spring.version>4.2.4.RELEASE</spring.version>
      	<cxf.version>3.2.0</cxf.version>
      </properties>
      
      <dependencies>
      
      	<!--spring -->
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-core</artifactId>
      		<version>${spring.version}</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-beans</artifactId>
      		<version>${spring.version}</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-context</artifactId>
      		<version>${spring.version}</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-context-support</artifactId>
      		<version>${spring.version}</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-test</artifactId>
      		<version>${spring.version}</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-web</artifactId>
      		<version>${spring.version}</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-webmvc</artifactId>
      		<version>${spring.version}</version>
      	</dependency>
      	
      	<dependency>
      		<groupId>org.apache.cxf</groupId>
      		<artifactId>cxf-rt-transports-http</artifactId>
      		<version>3.1.8</version>
      	</dependency> <!--web service 以下都是cxf必备的 --> <!--org.apache.cxf.transport.servlet.CXFServlet -->
      	<dependency>
      		<groupId>org.apache.cxf</groupId>
      		<artifactId>cxf-rt-transports-http</artifactId>
      		<version>3.1.8</version>
      	</dependency> <!--不加这个包会报错Unable to locate spring NamespaceHandler for XML schema namespace 
      		[http://cxf.apache.org/jaxws] -->
      	<dependency>
      		<groupId>org.apache.cxf</groupId>
      		<artifactId>cxf-rt-frontend-jaxws</artifactId>
      		<version>3.1.8</version>
      	</dependency> <!--java实现webservice,不部署到tomcat,需要jetty包支持 -->
      	<dependency>
      		<groupId>org.apache.cxf</groupId>
      		<artifactId>cxf-rt-transports-http-jetty</artifactId>
      		<version>3.1.8</version>
      	</dependency>
      </dependencies>
      
      
    • 配置web.xml,因为CXF是基于Servlet所以要配置对应的Servlet

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app version="2.5"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <display-name>webservice_server</display-name>
      <welcome-file-list>
      	<welcome-file>index.html</welcome-file>
      	<welcome-file>index.htm</welcome-file>
      	<welcome-file>index.jsp</welcome-file>
      	<welcome-file>default.html</welcome-file>
      	<welcome-file>default.htm</welcome-file>
      	<welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <context-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>classpath:applicationContext-service.xml</param-value>
      </context-param>
      <listener>
      	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <servlet>
      	<servlet-name>CXFService</servlet-name>
      	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      </servlet>
      <servlet-mapping>
      	<servlet-name>CXFService</servlet-name>
      	<url-pattern>/webservice/*</url-pattern>
      </servlet-mapping>
      
      </web-app>
      
    • 创建Spring配置文件 , CXF配置可以放在Spring配置文件中,但不要放在SpringMVC文件中

      <?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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:jaxws="http://cxf.apache.org/jaxws"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd 
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd 
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd
          http://cxf.apache.org/jaxws
          http://cxf.apache.org/schemas/jaxws.xsd">
      
      <!-- jax-ws endpoint定义 -->
      <bean id="webserviceServer" class="webservice.ServerToJava"/>
      <!--发布webservice-->
      <jaxws:endpoint id="myService" implementor="#webserviceServer" address="/web-publish"  />
          </beans>
      
    • 访问时的路径:http://localhost:8080/webservice_server/webservice/web-publish?wsdl 规则为:主机+项目+CXFServlet的访问路径+address,出现对应的wsdl页面即部署成功。

    客户端

    • 普通Java程序调用

      • 当自己有java的webservice的服务端,即拥有接口的时候,可以直接通过接口和地址进行调用

      public class ClientForCXF {
      public static MyWebService getInterFace() {
      JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
      factoryBean.setServiceClass(MyWebService.class);
      factoryBean.setAddress("http://localhost:8080/server/web-publish");
      return (MyWebService) factoryBean.create();
      }

        public static void main(String[] args) {
            MyWebService myWebService = getInterFace();
            System.out.println("client: " + myWebService.add(1, 3));
        }
      

      }

        ```
      
      • 而往往服务端可能由别的语言实现,或者服务端并非我们自己实现,我们没有服务端接口,我们只能获得暴漏的wsdl,并进行调用,这就需要使用wsdl2java生成该wsdl的java客户端并调用了

        • 首先将从官网下载的文件解压,并且配置环境变量为C:UsersAdministratorDownloadsapache-cxf-3.1.14in 即解压出来文件的bin目录

        • cmd中输入wsdl2java -help输出信息即表示配置完成, 注意,这里不能使用jdk的wsimport

        • cmd进入任意文件夹,运行命令wsdl2java -encoding utf-8 http://localhost:8080/webservice_server/webservice/web-publish?wsdl 然后将生成的文件复制到目录下,随后直接调用就可以

      public static void main(String[] args)
      ServerToJavaService javaService = new ServerToJavaService();
      HelloWorldInterface c = javaService.getServerToJavaPort();
      String a = c.sayHello("s");
      System.out.println(a);
      }
      ```

    • 通过Spring容器

      • 需要有CXF和Spring的jar

      • 先将接口(只是接口即可)生成出来放到项目中

      • 编写配置文件

            <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd 
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd 
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://cxf.apache.org/jaxws
            http://cxf.apache.org/schemas/jaxws.xsd">
        
        
        <jaxws:client id="crmClient" 
        	serviceClass="webservice_client.HelloWorldInterface"   <!-- 对应的接口-->
        	address="http://localhost:8080/webservice_server/webservice/web-publish"/> <!-- 地址 -->
        
        </beans>
        
      • 最后和使用平常的bean一样使用就可以

           public static void main(String[] args) {
                ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-service.xml");
                HelloWorldInterface c = (HelloWorldInterface) applicationContext.getBean("crmClient");
                String str = c.sayHello("终于等到你");
                System.out.println(str);
        
         }
        
      • 结果

    类似的框架还有Alibaba的开源项目Dubbo ,这是入门介绍http://www.cnblogs.com/liyuhui-Z/p/7799615.html

    参考文章http://www.imooc.com/article/14635

  • 相关阅读:
    单元测试——破除依赖
    单元测试——基础概念
    2015年9月书单推荐
    菜鸟vimer成长记——第4.2章、编程插件
    克服弱点,愈发完美-自我篇——《人性的弱点》读后感
    菜鸟vimer成长记——第4.0章、Vim插件管理利器-Vundle
    菜鸟vimer成长记——第3章、文件
    阿里IPO法律咨询费达1580万美元 为Facebook六倍
    新移民漫画家 以幻想构筑奇妙世界
    请教Amazon FBA里面Label Service, Stickerless, Commingled Inventory是什么意思?
  • 原文地址:https://www.cnblogs.com/liyuhui-Z/p/7806192.html
Copyright © 2011-2022 走看看