初识webservice

一.神秘的webservice

Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML标准通用标记语言下的一个子集)标准描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。

二.webservice技术支持

                                                        

(详情可参考webservice的百度百科)

Web Service平台需要一套协议来实现分布式应用程序的创建。任何平台都有它的数据表示方法和类型系统。要实现互操作性,Web Service平台必须提供一套标准的类型系统,用于沟通不同平台、编程语言和组件模型中的不同类型系统。这些协议有:

XML和XSD

可扩展的标记语言标准通用标记语言下的一个子集)是Web Service平台中表示数据的基本格式。除了易于建立和易于分析外,XML主要的优点在于它既与平台无关,又与厂商无关。XML是由万维网协会(W3C)创建,W3C制定的XML SchemaXSD 定义了一套标准的数据类型,并给出了一种语言来扩展这套数据类型

Web Service平台是用XSD来作为数据类型系统的。当你用某种语言如VB. NET或C# 来构造一个Web Service时,为了符合Web Service标准,所有你使用的数据类型都必须被转换为XSD类型。如想让它使用在不同平台和不同软件的不同组织间传递,还需要用某种东西将它包装起来。这种东西就是一种协议,如 SOAP。

SOAP

SOAP即简单对象访问协议(Simple Object Access Protocol),它是用于交换XML标准通用标记语言下的一个子集)编码信息的轻量级协议。它有三个主要方面:XML-envelope为描述信息内容和如何处理内容定义了框架,将程序对象编码成为XML对象的规则,执行远程过程调用(RPC)的约定。SOAP可以运行在任何其他传输协议上。例如,你可以使用 SMTP,即因特网电子邮件协议来传递SOAP消息,这可是很有诱惑力的。在传输层之间的头是不同的,但XML有效负载保持相同。

Web Service 希望实现不同的系统之间能够用“软件-软件对话”的方式相互调用,打破了软件应用、网站和各种设备之间的格格不入的状态,实现“基于Web无缝集成”的目标。

WSDL

Web Service描述语言WSDL 就是用机器能阅读的方式提供的一个正式描述文档而基于XML标准通用标记语言下的一个子集)的语言,用于描述Web Service及其函数、参数和返回值。因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的。

UDDI

UDDI 的目的是为电子商务建立标准;UDDI是一套基于Web的、分布式的、为Web Service提供的、信息注册中心的实现标准规范,同时也包含一组使企业能将自身提供的Web Service注册,以使别的企业能够发现的访问协议的实现标准。

三.为什么需要Web服务

Web服务为Internet上应用程序之间的交互提供了方便

Web服务也减轻了企业级应用中出现的异构系统的整合危机

Web服务的优势包括:

四.web广泛用到的技术

  1. TCP/IP:通用网络协议,被各种设备使用
  2. HTML标准通用标记语言下的一个应用):通用用户界面,可以使用HTML标签显示数据
  3. .NET: 不同应用程序间共享数据与数据交换
  4. Java:写一次可以在任何系统运行的通用编程语言,因为java具有跨平台特性
  5. XML标准通用标记语言下的一个子集):通用数据表达语言,在web上传送结构化数据的容易方法

他们的特点是其开放性,跨平台性,开放性正是Web services的基础。

五.Web服务在项目中的使用

1.使用JAX-WS发布和调用web服务

      (JAX-WS--->web服务标准,jdk中的一个组件,集成了JAXB,本质上其实是Scoket编程)

01.发布自己的ws服务

源码介绍:

HelloService.java

package cn.myservice;
//Service端(服务器端)
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

//局域网任何人都可以访问
@WebService
public class HelloService {

  public void say(String name){
      System.out.println("Hello"+name);
  }
  public static void main(String[] args) {
      /**
       * 端口号:50000
       * 一个标识(区分的作用):hello
       * 发布者:new HelloService()
       */
     Endpoint.publish("http://localhost:50000/hello", new HelloService());
     System.out.println("server is listening ....");
  }
}

运行效果:

大家也可以用cmd命令 netstat -na来看看有没有我们发布的端口号的存在(如下图,它是处于监听状态的)

现在我们的局域网上都可以访问我发布的(http://localhost:50000/hello)这个服务了

效果:

02.调用自己的ws服务

001.MyEclipse自带工具调用

步骤一:

步骤二:

步骤三:

步骤四:

步骤五:

步骤六:

步骤七:

这时就完成了调用,控制台就会打印相应的信息

002.书写代码调用

其中我们myservice包中的类我们是不需要自己去写的,我们可以使用jdk中的wsimport.exe利用我们cmd命令给我们生成(当然是在保证我们的jdk安装,环境变量配置成功的情况下)

操作如下:

这时,我们来看看我们的c盘根目录:

源码介绍:

1.HelloService.java

package cn.myservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 *
 */
@WebService(name = "HelloService", targetNamespace = "http://myservice.cn/")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface HelloService {

    /**
     *
     * @param arg0
     */
    @WebMethod
    @RequestWrapper(localName = "say", targetNamespace = "http://myservice.cn/", className = "cn.myservice.Say")
    @ResponseWrapper(localName = "sayResponse", targetNamespace = "http://myservice.cn/", className = "cn.myservice.SayResponse")
    @Action(input = "http://myservice.cn/HelloService/sayRequest", output = "http://myservice.cn/HelloService/sayResponse")
    public void say(
        @WebParam(name = "arg0", targetNamespace = "")
        String arg0);

}

2.HelloServiceService.java

package cn.myservice;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 *
 */
@WebServiceClient(name = "HelloServiceService", targetNamespace = "http://myservice.cn/", wsdlLocation = "http://localhost:50000/hello?wsdl")
public class HelloServiceService
    extends Service
{

    private final static URL HELLOSERVICESERVICE_WSDL_LOCATION;
    private final static WebServiceException HELLOSERVICESERVICE_EXCEPTION;
    private final static QName HELLOSERVICESERVICE_QNAME = new QName("http://myservice.cn/", "HelloServiceService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:50000/hello?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        HELLOSERVICESERVICE_WSDL_LOCATION = url;
        HELLOSERVICESERVICE_EXCEPTION = e;
    }

    public HelloServiceService() {
        super(__getWsdlLocation(), HELLOSERVICESERVICE_QNAME);
    }

    public HelloServiceService(WebServiceFeature... features) {
        super(__getWsdlLocation(), HELLOSERVICESERVICE_QNAME, features);
    }

    public HelloServiceService(URL wsdlLocation) {
        super(wsdlLocation, HELLOSERVICESERVICE_QNAME);
    }

    public HelloServiceService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, HELLOSERVICESERVICE_QNAME, features);
    }

    public HelloServiceService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public HelloServiceService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     *
     * @return
     *     returns HelloService
     */
    @WebEndpoint(name = "HelloServicePort")
    public HelloService getHelloServicePort() {
        return super.getPort(new QName("http://myservice.cn/", "HelloServicePort"), HelloService.class);
    }

    /**
     *
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns HelloService
     */
    @WebEndpoint(name = "HelloServicePort")
    public HelloService getHelloServicePort(WebServiceFeature... features) {
        return super.getPort(new QName("http://myservice.cn/", "HelloServicePort"), HelloService.class, features);
    }

    private static URL __getWsdlLocation() {
        if (HELLOSERVICESERVICE_EXCEPTION!= null) {
            throw HELLOSERVICESERVICE_EXCEPTION;
        }
        return HELLOSERVICESERVICE_WSDL_LOCATION;
    }

}

3.ObjectFactory.java

package cn.myservice;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

/**
 * This object contains factory methods for each
 * Java content interface and Java element interface
 * generated in the cn.myservice package.
 * <p>An ObjectFactory allows you to programatically
 * construct new instances of the Java representation
 * for XML content. The Java representation of XML
 * content can consist of schema derived interfaces
 * and classes representing the binding of schema
 * type definitions, element declarations and model
 * groups.  Factory methods for each of these are
 * provided in this class.
 *
 */
@XmlRegistry
public class ObjectFactory {

    private final static QName _SayResponse_QNAME = new QName("http://myservice.cn/", "sayResponse");
    private final static QName _Say_QNAME = new QName("http://myservice.cn/", "say");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: cn.myservice
     *
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link SayResponse }
     *
     */
    public SayResponse createSayResponse() {
        return new SayResponse();
    }

    /**
     * Create an instance of {@link Say }
     *
     */
    public Say createSay() {
        return new Say();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link SayResponse }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://myservice.cn/", name = "sayResponse")
    public JAXBElement<SayResponse> createSayResponse(SayResponse value) {
        return new JAXBElement<SayResponse>(_SayResponse_QNAME, SayResponse.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link Say }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://myservice.cn/", name = "say")
    public JAXBElement<Say> createSay(Say value) {
        return new JAXBElement<Say>(_Say_QNAME, Say.class, null, value);
    }

}

4.package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "http://myservice.cn/")
package cn.myservice;

5.Say.java

package cn.myservice;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

/**
 * <p>say complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="say">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="arg0" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "say", propOrder = {
    "arg0"
})
public class Say {

    protected String arg0;

    /**
     * 获取arg0属性的值。
     *
     * @return
     *     possible object is
     *     {@link String }
     *
     */
    public String getArg0() {
        return arg0;
    }

    /**
     * 设置arg0属性的值。
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *
     */
    public void setArg0(String value) {
        this.arg0 = value;
    }

}

6.SayResponse.java

package cn.myservice;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

/**
 * <p>sayResponse complex type的 Java 类。
 *
 * <p>以下模式片段指定包含在此类中的预期内容。
 *
 * <pre>
 * &lt;complexType name="sayResponse">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayResponse")
public class SayResponse {

}

7.MyTest.java(测试类)

package cn.test;

import cn.myservice.HelloService;
import cn.myservice.HelloServiceService;

public class MyTest {
    public static void main(String[] args) {
        HelloServiceService service = new HelloServiceService();
        HelloService port = service.getHelloServicePort();
        port.say("坤坤");
    }
}

8.运行效果

这就完成了调用。

 2.使用CXF发布和调用web服务

未完待续。。。

时间: 2024-08-05 03:11:54

初识webservice的相关文章

初识webservice 服务

1.获取电话号码归属地查询 首先访问: http://www.webxml.com.cn/zh_cn/web_services.aspx ①新建一个MyEclipse项目(WebService) http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 上面就可以查询到手机号归属地,下面我就带你进入编程环境中,如何获取到手机号码归属地 首先:上面的路径不能丢,有用滴,你先看看你的C盘根目录下有没有一个cn文件夹,若是有,你把它给删了,接

GXPT(六)——初识WebService

******含义: 是什么: 严格来说WebService是行业标准,也就是WebService规范,也称作WS-*规范,既不是框架,也不是技术. WebService即Web服务,因为网络而产生,发布Web服务后可以将资源进行共享,通过Webservice调用获取并操作资源信息. WebService是一种跨编程语言和操作系统平台的远程调用技术即跨平台远程调用技术. 采用标注SOAP(Simple Object Access Protocol)协议传输,soap属于W3C标注.基于HTTP传输

初识Web服务(即WebService)

耶 他当成菜鸟的话恐怕会挂得很惨地 Δ醪 须兼 恫宾 臀n镁 j〓佬ふ 眺瓶⒏孳 e啊怆垩 窨沼屡锘 砩典 捱①愀 迭竦翁粕 膑刖虱舭 箪澈镅 廪衙磊耖 放大看着就像是在看着来自地狱的魔鬼怎么 吹孚缤红 尢瘪飙冲 廉忸蘼灵 蟥岘 闩绚圮翳 怯躜╈ 箍蔸窦蓟 诛牢柱 埏祜篁钟 乌勰 伪Е候锱 扮讠肠 埙煜熄弑 蹿从悃幡 局尜茎综 乎是同时哼道:你少在这里跟我们装蒜我们 雇佣而来的从早上点一直杀到了下 低鳎衫啶 轩す 齿ㄉ懔蓑 铂烬贤困 鸳垌ゎ 烬醢尻

初识.Net Remoting

初识.Net Remoting.Net对于远程调用提供了两种方法:Remoting和WebService.WebService现在是如火如荼优点:1.Tcp通道的Remoting速度非常快2.虽然是远程的,但是非常接近于本地调用对象3.可以做到保持对象的状态4.没有应用程序限制,可以是控制台,winform,iis,windows服务承载远程对象缺点:1.不是标准的应用,因此有平台限制(不能想Webservice那样可以跨平台,只要Webservice内部方法能够返回json或者xml数据即可)

自定义及发布一个webservice服务

自定义及发布一个webservice服务    - 声明 某个业务服务为webservice服务       通过@webservice 注解来声明    - 发布webservice服务       Endpoint.publish()发布 (默认对public修饰的方法进行发布)    - 通过wsimport生成本地代理来访问自己发布的webservice       wsimport 1.发布自定义webservice phone.java package ws.myWebService

调用已发布的WebService

WebService服务演示 登录http://www.webxml.com.cn 单击手机查询服务 3.         选择要调用的方法 例如: getMobileCodeInfo. 4. 输入要查询的手机号单击”调用” 截图如下, 免费用户 UserID为null a)   可以看到返回如下结果: <?xml version="1.0" encoding="utf-8" ?> <string xmlns="http://WebXml

webservice实验一

实验目的:安装jdk1.6_21以后的版本,利用JAX-WS API自己发布webservice并调用,以及用wsimport生成webservice客户端代码调用一个免费的web服务(如webxml.com.cn上的获取手机归属地的服务). 一.webservice原理了解 webservice是一种通用的跨语言跨平台的数据交互方式,之所以能够做到这一点,是因为它的底层实现机制是依赖于HTTP协议以及XML格式这些开发的标准.webservice使用SOAP(simple object acc

C# 动态生成WebService,无需添加引用

C#项目调用WebService是很常见的现象,但一旦修改链接地址就需要重新更新引用很是麻烦,这里跟大家分享一个通过地址,无需添加引用动态生成Webservice的小方法 方法类: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ServiceModel; 6 using System.ServiceModel.Channels

初识Python,望君多多关照

在学习Python之前,我们接触过数据结构和网页制作.前者让我们学习如何把C语言运用的更加整齐规范,而后者让我们亲身学习如何运用所学,制作一个静态网页.通过这些课程的学习,让我对C语言产生了比较大的压力,以至于对编程.对这学期的Python课程都有一种如临大敌的感觉. 但是真的学习了这门课程,体会了编码过程中的一些固定运用方法和套路之后,也许过程中对这门课程隐隐约约产生了一点点朦胧的感觉,仿佛他也并没有想象中的那么困难,起码现在的学习让我认为,他可能没有C语言那么繁琐和麻烦.当然,以一个初学者的