首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >远程调用服务框架-CXF(WebServic)

远程调用服务框架-CXF(WebServic)

作者头像
李家酒馆酒保
发布2017-12-27 11:18:36
发布2017-12-27 11:18:36
1.8K0
举报
文章被收录于专栏:李家的小酒馆李家的小酒馆

介绍

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

规则

  • 基于http 协议
  • 传输的内容为xml格式
  • SOAP作为一个基于XML语言的协议用于在网上传输数据。
    • SOAP = 在HTTP的基础上+XML数据。
  • WSDL – WebService Description Language – Web服务描述语言。
    • 用来描述服务的地址和基本方法,java程序通过WSDL来生成对应的代理类来调用具体的方法

Java内置WebService实现

代码语言:txt
复制
服务端(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);     
        }  
  }
  • 客户端
    • 首先需要通过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

服务端
代码语言:txt
复制
编写接口
 @WebService 
 public interface HelloWorldInterface {
      String sayHello(String text); 
 }
代码语言:txt
复制
编写实现类
 //@WebService在实现类的注解让CXF知道WSDL创建时所使用的接口。 
 @WebService(endpointInterface = "webservice.service.HelloWorldInterface") 
 public class ServerToJava implements HelloWorldInterface {  
 
 public String sayHello(String text) {     
 return "hello" + text; 
 } 
 }
代码语言:txt
复制
导入项目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>
代码语言:txt
复制
配置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>
代码语言:txt
复制
创建Spring配置文件 , 
  • 访问时的路径:http://localhost:8080/webservice_server/webservice/web-publish?wsdl 规则为:主机+项目+CXFServlet的访问路径+address,出现对应的wsdl页面即部署成功。
客户端
  • 普通Java程序调用
代码语言:txt
复制
当自己有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)); 
 } 
 }   
代码语言:txt
复制
  public static void main(String[] args) 
  ServerToJavaService javaService = new ServerToJavaService(); 
  HelloWorldInterface c = javaService.getServerToJavaPort(); 
  String a = c.sayHello("s"); S
  ystem.out.println(a); 
  }
  • 通过Spring容器
    • 需要有CXF和Spring的jar
    • 先将接口(只是接口即可)生成出来放到项目中
代码语言:txt
复制
编写配置文件
     <?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>
代码语言:txt
复制
最后和使用平常的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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-11-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 规则
  • Java内置WebService实现
  • 使用CXF框架和Spring整合
    • 服务端
    • 客户端
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档