Webservice学习

1WebService有什么用,使用它有什么好处及它的原理是怎样的

Web Service主要为了使原来各个孤立的站点之间的信息能够相互通信、共享而提出的一种接口。 Web Service所使用的是Internet上统一、开放的标准,如HTTPXMLSOAP(简单对象访问协议 simple object access protocal)、WSDL等,所以Web Service可以在任何支持这些标准的环境(Windows,Linux)中使用。注:SOAP协议(Simple Object Access Protocal,简单对象访问协议),它是一个基于XML的通讯协议用于分散和分布式环境下网络信息交换。在此协议下,软件组件或应用程序能够通过标准的HTTP协议进行通讯。它的设计目标就是简单性和扩展性,这有助于大量异构程序和平台之间的互操作性,从而使存在的应用程序能够被广泛的用户访问。

 

2Webservice移植性

    是不同系统(不同语言开发的系统、相同语言但属于不同应用开发的系统)之间互操作的平台,他提供一种标准,不同平台和不同语言之间进行互操作(异构系统之间互连互通)的标准

它与语言和平台无关(无关性),有的时候无关性比性能更重要,意味着更通用

也可以认为webservice就是一种应用程序,不同的系统通过它可以是实现互操作

如果其它系统要访问发布在web服务器上java Web系统中某个类中的方法,这个时候只需要给该类封装成一个WebService Api接口

 

web server

(tomcat ,weblogic)

Java web项目

 

 

 

s

Webservice

    ApI

windows client

C client

Other platform

Soap response

Soap request

 

 

 

 

 

 


  

 

 

 

 

 

 

 

 

 

 

 

3、如果一个webservice创建好了之后就可以访问了

通过Soap(simple object access protocol)协议,实际使用了xml来描述和封装request和

response,使得他们(request和response)可以通过soap协议进行传递.

 

soap协议中提供了标准的远程过程调用RPC(方法)来访问webservice,rpc使用xml和xsd(xml schema Definition是DTD的替代品)来描述被请求的方法以及该方法的参数情况和返回情况{ 即用xml和xsd来描述和封装request和response,使得他们(request和response)可以通过soap协议进行传递}

4、为什么其它系统都可以访问webservice

  webservice使用xml和xsd来描述内部一些配置,xml和xsd是webservice基本数据格式

  wsdl(webservice description language),它使用的全是xml和xsd。它描述了webservice中一些方法访问的契约(规范),谁都遵守该规范,即它描述了web service中能被访问方法的名,参数,参数的类型和返回值等等规定,其它平台上的程序必须按照wsdl中的规范来访问

 

5、目前能使用的webService框架(这些框架是用java语言开发的)有:
  apache      开发的 Axis

Codehaus    开发的 Xfire

 

 

6、案例

XFire使用

       首先打开IDE,创建一个普通的Java工程,将Xfire1.2所需要的jar加入到工程的类库引用中。创建一个简单的Java文件,这个文件只有一个名为example的成员方法。

提供服务实现类

package org.itfuture.www.admin;

public class MessageServiceImpl implements IMessageService {

   

    public String example(String message)

    {

       return "我得到消息是:"+message;

    }

}

服务类的接口

package org.itfuture.www.admin;

public interface IMessageService

{

    public String example(String message);

}

配置services.xml

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

<beans xmlns="http://xfire.codehaus.org/config/1.0">

<service>

    <name>Message</name>

    <namespace>http://org.itfuture.www.admin/AAA</namespace>

    <serviceClass>org.itfuture.www.admin.IMessageService</serviceClass>

    <implementationClass>org.itfuture.www.admin.MessageServiceImpl</implementationClass>

    <style>wrapped</style>

    <use>literal</use>

    <scope>application</scope>

</service>

</beans>

标签说明:

(1)WebService的服务名

<name> Message </name>

(2)WebService的服务接口类

<serviceClass> org.itfuture.www.admin.IMessageService</serviceClass>

(3)WebService的服务接口实现类

<implementationClass>org.itfuture.www.admin.MessageServiceImpl</implementationClass>

(4)Soap中XML元素风格

<style>wrapped</style>

(5)soap消息中是否声明数据的数据类型

<use>literal</use>

(6)WebService的服务有效范围

<scope>application</scope>

 

 

关于<style>标签

Document 就是将SOAP请求和响应,或者说输入输出定义为XML元素,有严格的Schema("document" style means the messages in and out of the service are exactly as they are describe by the XML Schema in the WSDL).

Wrapped 风格就是在wsdl的xml中如果出现了定义操作有同名的Element,将参数作为 Child Element;这样操作名又重新回到了SOAP消息中

 

关于<use>标签

literal就是不在SOAP消息中声明数据的数据类型,而通过其它方式获知数据类型,这种方式是开发包相关的,没有什么标准;如<x>50</x>,单从SOAP消息,你无法判断50是数字还是字符串,而具体的类型可以在开发包将SOAP请求映射到具体的Service类时来确定并完成转换,对于返回值也一样,客户端可已通过SetReturnValueType(...)之类的方法告知开发包自己期待什么类型

encoding就是在SOAP消息中携带数据的类型信息,并且依据某种规则将数据编码传递,接收端可以根据类型信息和编码规则完成解码,获得原始数据;如<x xsi:type="xsd:string">50</x>

 

web.xml配置

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

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>ServiceApp</display-name>

 

    <servlet>

         <servlet-name>XFireServlet</servlet-name>

         <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet

         </servlet-class>

     </servlet>

     <servlet-mapping>

         <servlet-name>XFireServlet</servlet-name>

          <url-pattern>/services/*</url-pattern>

     </servlet-mapping>

</web-app>

测试服务(模拟一个远端异构客户端)

package org.itfuture.www.test;

 

import java.net.MalformedURLException;

 

import org.codehaus.xfire.XFireFactory;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.Service;

import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import org.itfuture.www.admin.IMessageService;

 

public class TestService{

  /**

   * 模拟一个java的客户端

   *

   */

    public TestService()

    {

    }

    public static void main(String[] args)

    { 

       //通过接口类创建Service对象

       Service srvcModel = new  ObjectServiceFactory().create(IMessageService.class);  

       //通过XFire的工厂类创建工厂对象

       XFireProxyFactory factory = 

new XFireProxyFactory(XFireFactory.newInstance().getXFire());

       //访问的地址

       String helloWorldURL ="http://192.168.2.66/ServiceApp/services/Message";

       //异常处理

     try{ 

          //创建服务对象

          IMessageService srvc =

(IMessageService)factory.create(srvcModel, helloWorldURL);

           //调用服务中的方法,并显示其结果

           String result = srvc.example("hello world");

           System.out.print(result);   

       }catch (MalformedURLException e) {    

            e.printStackTrace();   

       }

 

    } 

}

客户端访问代码

       XFireAxis以及其他的一些商业产品都提供了wsdl文档创建客户端代码的工具。这里采用XFire提供的wsgen工具来创建客户端的访问代码。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值