使用cxf框架 创建webService服务端和客户端调用webservice

什么是webservice

当你想要你的页面有一个天气的时候你会怎么做 不会说自己写一个能探测天气的app吧 肯定在别的地方整过来啊 webservice就是这样的东西 它能让你使用别人给你提供的服务。
Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。

XML:(Extensible Markup Language)扩展型可标记语言。面向短期的临时数据处理、面向万维网络,是Soap的基础。

Soap:(Simple Object Access Protocol)简单对象存取协议。是XML Web Service 的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,它可以支持不同的底层接口,像HTTP(S)或者SMTP。

WSDL:(Web Services Description Language) WSDL 文件是一个 XML 文档,用于说明一组 SOAP 消息以及如何交换这些消息。大多数情况下由软件自动生成和使用。

UDDI (Universal Description, Discovery, and Integration) 是一个主要针对Web服务供应商和使用者的新项目。在用户能够调用Web服务之前,必须确定这个服务内包含哪些商务方法,找到被调用的接口定义,还要在服务端来编制软件,UDDI是一种根据描述文档来引导系统查找相应服务的机制。UDDI利用SOAP消息机制(标准的XML/HTTP)来发布,编辑,浏览以及查找注册信息。它采用XML格式来封装各种不同类型的数据,并且发送到注册中心或者由注册中心来返回需要的数据。

调用原理

这里写图片描述
实现一个完整的Web服务包括以下步骤:

◆ Web服务提供者设计实现Web服务,并将调试正确后的Web服务通过Web服务中介者发布,并在UDDI注册中心注册; (发布)

◆ Web服务请求者向Web服务中介者请求特定的服务,中介者根据请求查询UDDI注册中心,为请求者寻找满足请求的服务; (发现)

◆ Web服务中介者向Web服务请求者返回满足条件的Web服务描述信息,该描述信息用WSDL写成,各种支持Web服务的机器都能阅读;(发现)

◆ 利用从Web服务中介者返回的描述信息生成相应的SOAP消息,发送给Web服务提供者,以实现Web服务的调用;(绑定)

◆ Web服务提供者按SOAP消息执行相应的Web服务,并将服务结果返回给Web服务请求者。(绑定)

webservice调用实例

使用cxf框架 java1.8 因为cxf需要spring支持 所以需要引入spring
1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>sun007</groupId>
  <artifactId>TestWeb</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>TestWeb Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <java.version>1.8</java.version>
    <!-- spring版本号 -->
    <spring.version>4.3.11.RELEASE</spring.version>
    <!-- CXF版本号 -->
    <cxf.version>3.1.4</cxf.version>

  </properties>



  <dependencies>
    <!-- spring核心包 -->
    <!-- springframe start -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</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-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- CXF webservice -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>${cxf.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>TestWeb</finalName>
    <defaultGoal>compile</defaultGoal>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->
        <excludes>
          <!--测试环境-->
          <exclude>conf/test/*</exclude>
          <!--生产环境-->
          <exclude>conf/production/*</exclude>
          <!--开发环境-->
          <exclude>conf/development/*</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/resources/conf/${profiles.active}</directory>
        <targetPath>conf</targetPath>
      </resource>
    </resources>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.3</version>
          <configuration>
            <source>${java.version}</source>
            <target>${java.version}</target>
            <showWarnings>true</showWarnings>
            <encoding>${project.build.sourceEncoding}</encoding>
            <compilerArguments>
              <verbose />
              <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
            </compilerArguments>
          </configuration>
        </plugin>
        <!-- 解决maven命令console出现中文乱码 -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.7.2</version>
          <configuration>
            <forkMode>once</forkMode>
            <argLine>-Dfile.encoding=UTF-8</argLine>
            <skipTests>true</skipTests>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  <distributionManagement>
    <repository>
      <id>nexus-releases</id>
      <url>http://192.168.1.22:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
      <id>nexus-snapshots</id>
      <url>http://192.168.1.22:8081/nexus/content/repositories/releases/</url>
    </snapshotRepository>
  </distributionManagement>

  <profiles>
    <!--生产环境-->
    <profile>
      <id>production</id>
      <properties>
        <profiles.active>production</profiles.active>
      </properties>
    </profile>
    <!--测试环境-->
    <profile>
      <id>test</id>
      <properties>
        <profiles.active>test</profiles.active>
      </properties>
    </profile>

    <profile>
      <id>development</id>
      <properties>
        <profiles.active>development</profiles.active>
      </properties>
      <activation><activeByDefault>true</activeByDefault></activation>
    </profile>
  </profiles>
</project>
  1. cxf框架有个配置文件 叫做cxf-servlet.xml 需要在spring中引入一下
<?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:context="http://www.springframework.org/schema/context"
       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://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd">
   <!--因为spring配置文件引入了这个配置文件 扫描cxf类-->
    <context:component-scan base-package="com" />
    <bean id="cxfdemo" class="com.serverimpl.IcxfserverImpl">
    </bean>
  <!--发布webservice服务 使用jaxws endpoint方式
  id是这个接口叫什么名字
  implementor是它的实现类
   address是范围这个接口服务的地址路径-->
    <jaxws:endpoint id="cxfService" implementor="#cxfdemo" address="/cxfserver" />
</beans>
  1. spring配置文件application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config />
    <!--引入cxf配置文件-->
    <import resource="../cxf-servlet.xml" />

</beans>
  1. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>cxfWebservice</display-name>
  <!--配置spring  引入spring配置文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--配置cxf  以及路径-->
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/webService/*</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

剩下的就是服务类还有服务类的实现方法

package com.server;
import javax.jws.WebService;
/**
 * Created by Administrator on 2018/4/18 0018.
 */
@WebService
public interface Icxfserver {
    String sayHello(String name);
}
package com.serverimpl;

import com.server.Icxfserver;
import javax.jws.WebService;
/**
 * Created by Administrator on 2018/4/18 0018.
 */
@WebService
public class IcxfserverImpl implements Icxfserver {
    @Override
    public String sayHello(String name) {
        return "hello "+name;
    }
}

然后就可以在http://localhost:8080/webService/cxfserver?wsdl 打开后就会有一个wsdl文件 说明服务搭建成功
这里写图片描述

客户端

客户端我写的比较简单 而且是调用自己发布的服务 建了一个java工程 创建一个类 把服务端的服务文件添加到客户端 然后就可以调用了
打开cmd 运行 命令: wsImport -keep http://localhost:8080/webService/cxfserver?wsdl 后面这是自己的路径 (在上面配置过)
这里写图片描述
然后去路径那里就会发现有java文件生成 copy到客户端(只拷贝service和service的实现类就可以)其中实现类拷贝进去应该有个地方报错 可以直接删除
然后

import service.Icxfserver;
import service.IcxfserverImplService;

/**
 * Created by Administrator on 2018/4/18 0018.
 */
public class testWeb {
    public static void main(String[] args) {
    IcxfserverImplService CXF = new IcxfserverImplService(){};
    Icxfserver server=CXF.getIcxfserverImplPort();
    System.out.print(server.sayHello("shenbaoyuan"));
    }
}

调用就会成功
这里写图片描述
这个其实就是理解一下webservice是怎么发布和调用的 计算机东西太多需要一直努力 一直理解 比如调用网上别人发布的服务 可能有更简单的方法 可以一起讨论 进步

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值