MyEclipse6Java开发中文教程 package hallows //Generated by MyEclipse public interface IHelloWorld t public String example(string message) 此接口定义了一个参数为 message,返回值为 String,名为 example的方法,这个方法 就是最终要对外暴露的Web服务,可以根据情况修改为其它功能,例如计算加法: public double add(double a, double b) 也可以是不带参数和返回值的方法。而类 Hello world则定义了其实现 package hellos //Generated by MyEclipse public class HelloWorldImpl implements IHelloWorld public String example(String message) return message 默认的实现很简单,就是把参数返回即可。际上到这里位置 Web Service服务端的开发 已经完成了,不过,我们还是把它稍微调整一下,修改下方法的实现代码 public String example(String message)( System. out. printin(this reum·你好这是我的第一个 Web service,你输入的消息是”+ message, 。这样就有两个目的,第一是能分辨出传入的参数和传出的参数:第二呢,是在控制台上打 印一行信息,便于跟踪服务器端所发生的事情。 1524发布运行项目,显示WsDL 现在,我们可以将这个应用发布到服务器上,或者在 Package Explorer视图中选中项 目节点 Helloworld service,然后选择菜单Run> Run as>3 My Eclipse Server Application,之后 MyEclipse可能会显示一个可用的服务器列表,选中其中的服务器之 例如 MyEclipseπ omat并点击oK按钮后,项目就会自动发布,对应的服务器会启动。如 果读者发现个别时候发布项目后报错,打不开,那可以把项目删除掉再重来一次编写,发布 就好了。 那么 Web service是不能直接测试的,我们要用下一小节所提到的 Web service浏览器 或者是编写一个客户端来进行测试。这个Web服务监听的地址是在 htto/ localhost:8080 Helloworld Service/ sevices/ Helloworld,当我们企图直接访问时,将 会得到一段报错信息: 刘长炯著
MyEclipse 6 Java 开发中文教程 11 刘长炯著 package hellows; //Generated by MyEclipse public interface IHelloWorld { public String example(String message); } 。此接口定义了一个参数为 message,返回值为 String,名为 example 的方法,这个方法 就是最终要对外暴露的 Web 服务,可以根据情况修改为其它功能,例如计算加法: public double add(double a, double b); 也可以是不带参数和返回值的方法。而类 HelloWorld 则定义了其实现: package hellows; //Generated by MyEclipse public class HelloWorldImpl implements IHelloWorld { public String example(String message) { return message; } } 默认的实现很简单,就是把参数返回即可。实际上到这里位置 Web Service 服务端的开发 已经完成了,不过,我们还是把它稍微调整一下,修改下方法的实现代码: public String example(String message) { System.out.println(this); return "你好, 这是我的第一个 Web Service, 你输入的消息是:" + message; } 。这样就有两个目的,第一是能分辨出传入的参数和传出的参数;第二呢,是在控制台上打 印一行信息,便于跟踪服务器端所发生的事情。 15.2.4 发布运行项目, 显示 WSDL 现在,我们可以将这个应用发布到服务器上,或者在 Package Explorer 视图中选中项 目节点 HelloWorldService,然后选择菜单 Run > Run As > 3 MyEclipse Server Application,之后 MyEclipse 可能会显示一个可用的服务器列表,选中其中的服务器之一 例如 MyEclipse Tomcat 并点击 OK 按钮后,项目就会自动发布,对应的服务器会启动。如 果读者发现个别时候发布项目后报错,打不开,那可以把项目删除掉再重来一次编写,发布 就好了。 那么Web Service是不能直接测试的,我们要用下一小节所提到的Web Service浏览器 或者是编写一个客户端来进行测试。这个 Web 服务监听的地址是在: http://localhost:8080/HelloWorldService/services/HelloWorld ,当我们企图直接访问时,将 会得到一段报错信息:
MyEclipse6Java开发中文教程 Invalid SOAP request. 即:无效的SOAP请求。然而,按照Web服务的规范,所有的Web服务都必须提供WsDL 描述,那么这里的Web服务也支持,只需要加入?wsd参数就可以了。访问如下地址 http://localhost:8080/helloworldSErvice/serviceS/helloworld?wsdl得到的输出如下(可 以看出WSDL的确不是给人看的) <?xml version="1.0"encoding=UTF-8"?> <wsdl: definitions targetnamespace=htTp //hellows xminstns="http:/hellows xminswsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/ xminssoap12=http.://www.w3.org/2003/05/soap-envelope xminsxsd=http://www.w3.org/2001/xmlschema xminssoapenc11="http://schemas.xmlsoap.org/soaplencodin xminssoapenc12="http://www.w3.org/2003/05/soap-encoding xminswsdl=http:schemas.xmlsoap.org/wsarenvelope xminssoap11=http://schemas.xmlsoap.org/soap/e <wsdl: types xsd schema xminsxsd=http://www.w3.org/2001/xmlschema attributeFormDefault="qualified elementFormDefault="qualified targetnamespace="hTtp: //hellows"> <xsd: element name="example"> ≤ xsd: complexType> <xsd sequence> xsd element maxOccurs=1 name="inO nillable="true ype="xsd: string"/> <hxsd complexType> </xsd: element> <xsd: element name="exampleResponse"> ≤ xsd: complexType> <xsd: sequence> <xsd: element maxOccurs=1" minoccurs= name="out" nillable="true type ng </ sequence> <lsd: complex Type> </xsd: schema> </sdl: types> <wsdl: message name="exampleRequest"> <wsdl: part name="parameters"element="tns: example"> <wsdl: message name="exampleResponse"> <wsdl: part name="parameters"element="tns exampleResponse"> </sdl message> 刘长炯著
MyEclipse 6 Java 开发中文教程 12 刘长炯著 Invalid SOAP request. 即:无效的 SOAP 请求。然而,按照 Web 服务的规范,所有的 Web 服务都必须提供 WSDL 描述,那么这里的 Web 服务也支持,只需要加入?wsdl 参数就可以了。访问如下地址: http://localhost:8080/HelloWorldService/services/HelloWorld?wsdl ,得到的输出如下(可 以看出WSDL的确不是给人看的): <?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://hellows" xmlns:tns="http://hellows" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://hellows"> <xsd:element name="example"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="exampleResponse"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="exampleRequest"> <wsdl:part name="parameters" element="tns:example"> </wsdl:part> </wsdl:message> <wsdl:message name="exampleResponse"> <wsdl:part name="parameters" element="tns:exampleResponse"> </wsdl:part> </wsdl:message>