您的位置:首页 > 其它

JAX-RPC学习笔记(2)-通过动态代理客户端访问webservice

2011-08-02 23:20 751 查看
上一节里花了很长时间来搭建第一个helloWorld程序,并完成了客户端对服务器端的调用,过程是痛苦的,结果是美好的:)

但是上一节的客户端调用程序也反应出来一些问题

a).客户端必须依赖一个服务器端提供的Stub包

b).服务器端升级并没有对客户端透明--因为你需要升级对应的stub包

这两个问题在这一节里将得到答案

1.开始前先来看看那个神秘的.wsdl文件吧

概念:Web Services Description Language

作用:给一个类添加一些描述信息,包裹一下,让其可以对外提供service

举个通俗的例子:超人(WebService)是怎么来的?就是一个普通人(普通的java接口,类),然后有一条神奇的可以外穿的内裤(WSDL),穿好后(WebService发布)就可以到处飞了(对外提供服务),并不是所有的内裤套在外面都能变超人的,内裤必须有一些独特的特性(WSDL中的各个元素)

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

<definitions name="MyHello" targetNamespace="http://www.crazycoder2010.com/wsdl/MyHello"
xmlns:tns="http://www.crazycoder2010.com/wsdl/MyHello"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<types/>
<message name="Hello_sayHello">
<part name="String_1" type="xsd:string"/></message>
<message name="Hello_sayHelloResponse">
<part name="result" type="xsd:string"/></message>
<portType name="Hello">
<operation name="sayHello" parameterOrder="String_1">
<input message="tns:Hello_sayHello"/>
<output message="tns:Hello_sayHelloResponse"/></operation></portType>
<binding name="HelloBinding" type="tns:Hello">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://www.crazycoder2010.com/wsdl/MyHello"/></input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://www.crazycoder2010.com/wsdl/MyHello"/></output>
</operation>
</binding>
<service name="MyHello">
<port name="HelloPort" binding="tns:HelloBinding">
<soap:address location="REPLACE_WITH_ACTUAL_URL"/></port>
</service>
</definitions>


2.JAX-RPC提供通过动态代理客户端方式访问服务器端程序,上图先



这种方式,客户端必须知道服务器的WSDL的位置,然后要调用的服务在哪个命名空间的哪个服务的哪个port下(好多哪个~~),看下上面的wsdl文件,其实就是从这个wsdl中定位唯一的一个服务节点。

知道了原理,然后我们就可以来编写我们的客户端程序了,注意,虽然我们不需要依赖Stub包了,但是以为我们是远程调用服务器上的接口,因此服务器端接口还是要添加到工程的classpath中(通常做法会把这些接口单独压一个jar包丢给客户端)

package com.crazycoder2010.jaxrpc;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;

public class HelloClient2 {
public static void main(String[] args)throws
MalformedURLException, ServiceException, RemoteException {
String urlString = "http://localhost:8080/hello/helloKevin?WSDL";//这个是服务器端发布成功后wsdl文件的访问链接
String nameSpace = "http://www.crazycoder2010.com/wsdl/MyHello";//注意和wsdl中的保持一致
String portName = "HelloPort";//保持一致
String serviceName = "MyHello";//还是保持一致:)
URL helloWsdlUrl = new URL(urlString);
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service service = serviceFactory.createService(helloWsdlUrl,new QName(nameSpace,serviceName));//定位到服务器的某个服务上
Hello hello = (Hello)service.getPort(new QName(nameSpace, portName),Hello.class);//定位到该服务下一个具体的服务绑定上(EndPoint)
System.out.println(hello.sayHello("Kevin03"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: