`
huxiangsky
  • 浏览: 1316 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

WebServices个人随笔

阅读更多
SOA与Web Service的关系,这是经常被混淆的两个概念。
SOA(Service-oriented architecture,面向服务的架构)。
将 企业应用系统中细粒度的功能打包成粗粒度的服务,通过中间方式(语言),
实现异构系统(操作系统、开发语言)之间的通讯、调用。 

Web Services是一种面向服务的架构的技术,
(SOA并不是一种技术,而是一种软件设计理念.Web service != SOA,但Web service 是目前实现SOA的最好方式。)
向外界暴露出一个能够远程进行调用的程序,主要目的是实现不同平台的应用服务可以互操作。
为了实现这一目标,Web Service 完全基于XML(可扩展标记语言)独立于平台、独立于开发语言的标准.
是创建可互操作的、分布式应用程序的新平台。

Web Services平台的技术。
Soap:(Simple Object Access Protocol)简单对象访问协议。是XML Web Service 的通信协议。
WSDL:(Web Services Description Language) WSDL 文件是一个 XML 文档,它对web Services的接口进行了定义 .
UDDI是一种查找相应服务的机制,用于web Services的发布、浏览以及查找注册信息.

架构(如何工作的)
服务提供者到注册中心(uddi)注册自己的服务。服务请求者(客户端)到注册中心(uddi)找到需要的web服务,
并获得服务定义(wsdl),根据wsdl定义就可以调用相应的服务(通过soap协议)。

Web Service的主要目标是跨平台的可互操作性。
    优点: 因为Soap协议,所以可以跨防火墙的进行通信 、
应用程序集成 (实现了异构系统) 、
因为不同平台的应用服务可以相互操作,所以实现了软件和数据重用
完全基于xml可扩展标记语言,所以跨语言 跨平台
  缺点:通过http协议进行远程调用, 访问速度慢、稳定性问题。

Web Service也有框架:如axis1,axis2、xfire、cxf。

Apache Axis2 是Axis的后续版本,是新一代的SOAP引擎。
Axis2的主要特点有:
(1)   采用名为 AXIOM(AXIs Object Model)的新核心 XML 处理模型,利用新的XML解析器提供的灵活性按需构造对象模型。
(2)   支持不同的消息交换模式。目前Axis2支持三种模式:In-Only、Robust-In和In-Out。In-Only消息交换模式只有SOAP请求,而不需要应答;
Robust-In消息交换模式发送SOAP请求,只有在出错的情况下才返回应答;In-Out消息交换模式总是存在SOAP请求和应答。
(3) 提供阻塞和非阻塞客户端 API。
(4)* 支持内置的 Web服务寻址 (WS-Addressing) 。
(5)   灵活的数据绑定,可以选择直接使用 AXIOM,使用与原来的 Axis 相似的简单数据绑定方法,
或使用 XMLBeans、JiBX 或 JAXB 2.0 等专用数据绑定框架。
(6) 新的部署模型,支持热部署。
(7)* 支持HTTP,SMTP,JMS,TCP传输协议。
(8) 支持REST (Representational State Transfer)。

axis2是一个web service框架,以下是使用中的一些总结:

1、要传递的对象首先必须实现serializable接口

2、axis2不支持方法重载。
     在对外发布的接口中不能使用方法重载,
     如果你写了多个重名的方法,在该service中只会注册一个。
     解决方法:更改成不同的方法名

3、axis2不支持事务。
     数据库增删改查一般都需要事务处理的,但是在对外发布的接口中是不能配置事务的。
     解决方法:或者写一个代理类或者直接在dao中进行事务管理。

4、AXIS2 1.4.1不支持java.util.Date type
     Time portion of java.util.Date is missing from SOAP response in Axis2 1.5
     瞧瞧axis2 的这些个bug,使用起来太不方便了。
     虽然Apache的官方网站的bug页上有解决方案,但是我把DateService.aar文件考下来后仍然报相同的错误:

    Axis2 1.5只能返回日期部分,时间部分丢失了!
    解决方法:把Date类型都改成String类型的,这样是最安全的,但可能跟要求不符,应该不能算是一个好的解决方案吧。

5、axis2在java中不支持二进制对象的传递如inputSream,。
    解决方法:将二进制流转换为只能传递byte[]。

6、axis2不能传递list、Map 类型的数据集合。
    解决方法:将list转换成数组类型。

项目中如何使用:
在我们的项目中,首先是将项目分布成几个小模块,对这些小的模块进行编码.
编码完成以后在通过webService连接起来,进行测试,这样做的好处就是





--------------------------web service    自己配置  <<服务器>>----------------------

首先建立一个动态工程->(导入Axis2的axis2.war包内的)lib目录下的所有的包->
在WebContent下导入A包内的axis2-web目录->在WEB-INF下面建立services目录->
在Src目录下建立class类->在services目录下建立(类名)的目录->建立META-INF->倒入A包的Version-1.4.1.aar包内的services.xml文件
->修改成.
---------------在 自己的机子上访问可以使用----------------------
<service name="hello">   //hello 可以随便取..
    <description>
        This service is to get the running Axis version
    </description>
    <parameter name="ServiceClass">com.HelloTest</parameter>   //类的地址
    <operation name="getVersion">
    <messageReceiver  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
</service>
------------------------------------------别人也来访问,也可以自己访问----------------------
  <service name="hello">
<Description>
Please Type your service description here
</Description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
<parameter name="ServiceClass" locked="false">com.HelloTest</parameter>
</service>
然后运行 该工程....在地址后面加入.services/listServices  比如:http://localhost:8080/Axis2Service/services/listServices
此时的画面会出现hello一个超链接.  可以点击的.

------------------------------------自己配置  客户端-----------------------------------
项目在:eclipseworkspace\axis2\Axis2Client
建立一个工程->导入服务端的所有包->src下面建立User的属性类,必须配置空构造方法->建立junit测试类
package com;

import javax.xml.namespace.QName;

import junit.framework.TestCase;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

/**
* web services 测试程序
*/
public class TestWebService extends TestCase {

private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR;

public void testWS() {
final String endPointReference = "http://localhost:8080/Axis2Service/services/hello";
final String targetNamespace = "http://com"; // com  服务端的包名(是倒过来的服务端是com.lxit.axis 如:axis.lxit.com)
init(endPointReference);

String opName = "sayHello"; //服务端类的方法名
Object[] opArgs = new Object[] { "第一次使用Web Service" };

Class<?>[] opReturnType = new Class[] { String[].class };

try {
Object[] response = invokeOp(targetNamespace, opName, opArgs,
opReturnType);
System.out.println(((String[]) response[0])[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
public void testLogin(){
final String endPointReference = "http://localhost:8080/Axis2Service/services/hello";
final String targetNamespace = "http://com"; //// com  服务端的包名(是倒过来的服务端是com.lxit.axis 如:axis.lxit.com)
init(endPointReference);

String opName = "login"; //服务端类的方法名
Object[] opArgs = new Object[] { "第一次使用Web Service","123456" };
//和上面唯一不同的是  这里是User.class ,而不是String[]
Class<?>[] opReturnType = new Class[] { User.class };

try {
Object[] response = invokeOp(targetNamespace, opName, opArgs,
opReturnType);
System.out.println(((User) response[0]).getName());
} catch (Exception e) {
e.printStackTrace();
}
}

public void init(String endpoint) {
try {
serviceClient = new RPCServiceClient();
options = serviceClient.getOptions();
targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
} catch (AxisFault e) {
e.printStackTrace();
}
}

private Object[] invokeOp(String targetNamespace, String opName,
Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
ClassNotFoundException {
// 设定操作的名称
QName opQName = new QName(targetNamespace, opName);
// 设定返回值
// Class<?>[] opReturn = new Class[] { opReturnType };
// 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
}

}
//////总之要注意的是: 如果服务端,没有开启,客户端是不会有任何效果的....
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics