zoukankan      html  css  js  c++  java
  • Java:Web Service初入门

    前言

    Web Service技术在我第一次接触,又没有实际使用时完全不理解这是什么。以为是一种类似Spring,Shiro的编程框架。后来渐渐理解,WS(即Web Service缩写)是一种通用的接口规范,并按照该规范编写接口对外提供服务。

    一 啥是WS

    这个问题在我没有编写WS代码时可是困扰了我很久,甚至第一次需要写WS接口都不知道老大到底要我写什么。因为我习惯于去网上寻找资料自学并实践某些知识点,而我在百度时查到的WS介绍基本都是这样的。

    百度百科:

    Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。

    百度搜索WS排名前列的博客:

    Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。

    这种解释对于不了解WS的人来看完全是一头雾水,后来在实践中渐渐明白了什么是WS。自我总结如下:

    Web Service就是一种跨编程语言跨操作系统平台远程调用技术。所谓跨编程语言和跨操作平台,就是说服务端程序采用Java编写,客户端程序则可以采用其他编程语言编写,反之亦然。跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。 远程调用,就是一台计算机的应用可以调用其他计算机上的应用,也可以直接理解为接口。例如:支付宝,支付宝并没有银行卡等数据,它只是去调用银行提供的接口来获得数据。还有天气预报等,也是气象局把自己的系统服务以webservice服务的形式暴露出来,让第三方网站和程序可以调用这些服务功能。

    所以Web Servic就是一种跨语言跨平台的接口技术。

    二 Spring整合CXF

    Web Service只是一套接口规范,实际运用中实现Web Service的方法有很多种,Java本身在jdk1.7之后也对webservice有了默认的实现(Oracle JAX-WS RI),但是在我们实际开发中一般还是会使用框架来,比如这里所提到的CXF就有着广泛的应用。
    CXF单独发布就不说了,直接讲Spring整合CXF,毕竟现在的JavaEE开发是离不开Spring了。

    2.1 maven配置

    加入maven依赖

    <!--cxf-->
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.6</version>
    </dependency>
    

    2.2 配置 web.xml

    <!--定义一个cxf的servlet-->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>
    

    2.3 写一个WS接口及其实现

    非常简洁的接口和实现
    接口:HelloWorld.java

    package com.erictao.cxf;
    import javax.jws.WebService;
    @WebService
    public interface HelloWorld {
    	public String say(String str);
    }
    

    实现:HelloWorldImpl.java

    package com.erictao.cxf.impl;
    import com.erictao.cxf.HelloWorld;
    import org.springframework.stereotype.Component;
    import javax.jws.WebService;
    @Component("helloWorld")
    @WebService
    public class HelloWorldImpl implements HelloWorld {
    	public String say(String str) {
    		return "Hello"+str;
    	}
    }
    

    2.4 修改Spring配置文件

    在spring主配置文件spring.xml中添加引入spring-cxf.xml

    <!-- lang: 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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
     
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     
     
    http://www.springframework.org/schema/context
     
     
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     
        <context:component-scan base-package="com.erictao"/>
        <!-- 省略其他配置内容 -->
        <import resource="spring-cxf.xml"/>
    </beans>
    

    并且创建配置文件spring-cxf.xml

    <!-- lang: 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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
     
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     
     
    http://cxf.apache.org/jaxws
     
     
    http://cxf.apache.org/schemas/jaxws.xsd">
     
        <!-- 定义webservice的发布接口  -->
        <jaxws:endpoint implementor="#helloWorld" address="/HelloWorld">
     
    </beans>
    

    更加具体的配置可以查看官方给出的文档:http://cxf.apache.org/docs/how-do-i-develop-a-service.html。
    #helloWorld指的是我们在HelloWorldImpl类中所自定义的名字,即@Component("helloWorld")定义的bean id,/HelloWorld则是我们定义的接口地址。

    三 运行发布WS

    之后我们运行项目输入该地址:http://127.0.0.1:8080/ssm/webservice/HelloWorld?wsdl如果出现如下界面:

    则说明我们的webservice发布成功了。接下来只需要通过客户端调用这个接口即可获得返回结果了。客户端的代码可以直接使用工具生成,可参考:https://blog.csdn.net/andyliulin/article/details/53680915。

    总结
    以上就是一个简单的Web Service入门实例,更多的关于CXF拦截器,客户端调用就没有做过多介绍,后续有时间的话再接着更新,明白了Web Service服务端和客户端怎么构建,后续只需要当做普通接口编写业务代码即可。

    该文章原创自EricTao

    博客:https://www.erictao2.com/

    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 如有问题, 可邮件(erictaotwo@gmail.com)咨询。
  • 相关阅读:
    Eclipse安装Hadoop插件
    (转)Ubuntu14.0.4中hadoop2.4.0伪分布模式配置
    Hadoop--DataNode无法启动
    启动与关闭hadoop
    hadoop中执行命令时发生错误
    strings命令
    Deriving data from ElasticSearch Engine
    elasticsearch data importing
    reading words in your computer and changing to female voice, linux festival text2wave saving wav files
    DDNS client on a Linux machine
  • 原文地址:https://www.cnblogs.com/taojinxuan/p/11136340.html
Copyright © 2011-2022 走看看