上一节里花了很长时间来搭建第一个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"));
}
}
分享到:
相关推荐
JAX-WS自学笔记 本人自学JAX-WS笔记和简单例子,文档标题结构如下: JAX-WS使用教程 1、JAX-WS概述 2、创建Web Service 2.1 从java开始 2.1.1 运行wsgen 2.1.2 生成的WSDL和XSD 2.1.3 目录结构 2.2 从WSDL...
JAX-WS自学笔记 本人自学JAX-WS笔记和简单例子,文档标题结构如下: JAX-WS使用教程 1、JAX-WS概述 2、创建Web Service 2.1 从java开始 2.1.1 运行wsgen 2.1.2 生成的WSDL和XSD 2.1.3 目录结构 ...
基于jax-ws 实现的web service client和server端的demo程序。 注:如果使用的是 myeclipse 时 server 部署到tomcat 启动的时候会报错 解决办法:找到myeclipse安装目录下的 plugins 目录里 查找 webservices-rt.jar,...
jax-rpc soap头扩展 客户端,服务端例子 ws-tipjax2SOAPHandler.ear,ws-tip-extend.zip 官方也有下载的
用JAX-RPC构建Web服务.docx
jax-ws客户端与服务端工程.rar-------------
J2EE Web Services: XML SOAP WSDL UDDI WS-I JAX-RPC JAXR SAAJ JAXP
MyEclipse8_0中使用 JAX-WS 部署 WebService 实例 - 外衣 - 博客频道 - CSDN_NET.mht
用JAX-RPC构建Web服务.pdf
webService部署tomcat需要的jax-ws 的完整jar包
JAX-WS方式开发和部署webservice应用,JAX-WS方式开发和部署webservice应用,JAX-WS方式开发和部署webservice应用,JAX-WS方式开发和部署webservice应用,JAX-WS方式开发和部署webservice应用
jax-ws webservice完整demo,包含所有jax-ws 2.2jar包。
NULL 博文链接:https://qidaoxp.iteye.com/blog/610149
使用JAX-WS(JWS)发布WebService 使用myeclipse开发java的webservice的两种方式 方式一: (此方式只能作为调试,有以下bug:jdk1.6u17?以下编译器不支持以Endpoint.publish方式发布document方式的soap,必须在...
NULL 博文链接:https://liuyunlong1229.iteye.com/blog/1678559
公司代码实现,简单的webservice服务 发布,实现客户端的调用。
The RESTEasy workbook that follows provides step-by-step instructions for installing, configuring, and running several working JAX-RS examples, using the JBoss RESTEasy implementation of JAX-RS 2.0. ...
[奥莱理] RESTful Java 开发 (Jax-RS 实现) (英文版) [奥莱理] RESTful Java with Jax-RS (E-Book) ☆ 图书概要:☆ Learn how to design and develop distributed web services in Java using RESTful ...