webService学习记录-01

webService 学习记录

根据网上视频学习整理,如有不对的地方,请大神们不吝赐教。


WebService和Web服务器有什么区别呢?
我们可以把WebService看作是Web服务器上应用;反过来说,Web服务器是WebService运行时所必需的容器。这就是它们的区别和联系。
l
使用JDK1.6发布的简单Web服务,其内部其实是使用 Socket实现。可以查看:SUN公司未对外公布的API类 com.sun.xml.internal.ws.transport.http.server . ServerMgr获知,请使用反编译工具。
l
WebService的特点
WebService通过HTTP POST方式接受客户的请求
WebService与客户端之间一般使用SOAP协议传输XML数据.
它本身就是为了跨平台或跨语言而设计的。

1.webService 是应用程序之间的远程调用,可以跨语言;

顾名思义就是基于Web的服务。 它使用 Web(HTTP) 方式,接收和响应外部系统的某种请求。 从而实现远程调用 .

2.我们可以调用互联网上查询天气信息Web服务,然后将它嵌入到我们的程序(C/S或B/S程序)当中来,当用户从我们的网点看到天气信息时,他会认为我们为他提供了很多的信息服务,但其实我们什么也没有做, 只是简单调用了一下服务器上的一段代码而已
学习WebService可以将你的服务(一段代码)发布到互联网上让别人去调用,也可以调用别人机器上发布的WebService,就像使用自己的代码一样


3.需要了解的几个名词

①XML,

②WSDL(WebServiceDescription Language) – Web服务描述语言。

可以理解为WebService的使用说明书,说明服务在哪里,如何调用

通过XML形式说明服务在什么地方-地址。

通过XML形式说明服务提供什么样的方法– 如何调用

如下的WSDL文件

<?xml version="1.0" encoding="UTF8" ?> 
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.itcast.cn/"
	name="HelloServiceService">
	<types>
		<xsd:schema>
			<xsd:import namespace="http://ws.itcast.cn/"
				schemaLocation="http://localhost:9999/hello?xsd=1" />
		</xsd:schema>
	</types>
	<message name="sayHi">
		<part name="parameters" element="tns:sayHi" />
	</message>
	<message name="sayHiResponse">
		<part name="parameters" element="tns:sayHiResponse" />
	</message>
	<portType name="HelloService">
		<operation name="sayHi">
			<input message="tns:sayHi" />
			<output message="tns:sayHiResponse" />
		</operation>
	</portType>
	<binding name="HelloServicePortBinding" type="tns:HelloService">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document" />
		<operation name="sayHi">
			<soap:operation soapAction="" />
			<input>
				<soap:body use="literal" />
			</input>
			<output>
				<soap:body use="literal" />
			</output>
		</operation>
	</binding>
	<service name="HelloServiceService">
		<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
			<soap:address location="http://localhost:9999/hello" />
		</port>
	</service>
</definitions>



③SOAP-Simple Object Access Protocol(简单对象访问协议)

SOAP作为一个基于XML语言的协议用于有网上传输数据。
SOAP = HTTP 的基础上 +XML 数据。
SOAP是基于HTTP的。
•SOAP的组成如下:
Envelope – 必须的部分。以XML的根元素出现。
Headers– 可选的。
Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。
WebService的请求都是post方式的,需要传输请求体。
类似如下:
POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	           xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
			   xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap:Body>
</soap:Envelope>



4.如何发布一个WebService服务。
与Web服务相关的类,都位于javax.jws.*包中。
主要类有:类里加注解才能发布
@ WebService - 它是一个注解,用在类上指定将此类发布成一个 ws .
Endpoint – 此类为端点服务类,它的方法 publish 用于将一个已经添加了 @ WebService 注解对象绑定到一个地址的端口上。

package cn.itcast.ws;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService /*需要加注解,只有加了注解的类,才能发布为Web服务*/
public class HelloService {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
       /*发布方法
		参数1:服务的地址
		参数2:服务的实现者
		*/
		Endpoint.publish("http://127.0.0.1:6789/hello", new HelloService());
		
	}
	
	public String sayHello(String name){
		
		return "Hello" +name;
		
	}

}


出现的错误:未加sayHello方法之前会出现错误

Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: The web service defined by the class cn.itcast.ws.HelloService does not contain any valid WebMethods.
at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(Unknown Source)
at com.sun.xml.internal.ws.server.EndpointFactory.createSEIModel(Unknown Source)
at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(Unknown Source)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown Source)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown Source)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(Unknown Source)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(Unknown Source)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(Unknown Source)
at javax.xml.ws.Endpoint.publish(Unknown Source)
at cn.itcast.ws.HelloService.main(HelloService.java:17)


因为在java中没有对外提供有效的方法,main方法的static的不能被外部调用发布的,不支持static


的,final方法,我们可以提供一个有效的方法对外发布(sayHello)方法,

运行程序就可以,

那么怎么看发布的WSDL文件呢?

拷贝发布的地址,找浏览器打开,粘贴在发布地之后加?wsdl即可。如:http://127.0.0.1:6789/hello?wsdl,就可以看本服务的WSDL文件;


服务发布之后,剩下的就是客户端调用的问题了

Endpoint.publish()方法执行时会启动一个新的线程,用于监听客户端的请求,之后的方法也会执行,多线程执行
若暂时不想将某一方法发布,可以在方法前加@WebMethod(exclude = true)注解


5.客户端如何调用服务费发布的方法


通过wsimport生成本地代码,调用网络上的web服务

客户端代码我们可以用wsimport命令自动生成,拷贝到eclipse中然后调用即可

wsimport是jdk自带的,可以根据wsdl文档生成客户端调用代码的工具.当然,无论服务器端的WebService是用什么语言写的,都将在客户端生成Java代码.服务器端用什么写的并不重要.
wsimport.exe位于JAVA_HOME\bin目录下.
l常用参数为:
-d<目录> - 将生成.class文件。默认参数。
-s<目录> - 将生成.java文件。
-p<生成的新包名> -将生成的类,放于指定的包下。
(wsdlurl)- http://server:port/service?wsdl,必须的参数。

示例:

C:/>wsimport –s .http://192.168.0.100/one?wsdl

注意:-s不能分开,-s后面有个小点,用于指定源代码生成的目录。点即当前目录。

如果使用了-s参数则会在目录下生成两份代码,一份为.class代码。一份为.java代码。

.class代码,可以经过打包以后使用。.java代码可以直接Copy到我们的项目中运行。

出的代码中会按照默认的包结构进行导出,如想自己定义包结构可以用 wsimport -s . -p com.test http://192.168.0.100/one?wsdl命令。


将生成的客户端代码拷贝到eclipse中之后,可以自己定义java类,调用发布的方法。

代码如下:

package cn.itcast.ws;
/**
 * 通过wsimport命令生产客户端代码调用webService服务
 * @author wangz_ing
 *
 */
public class App {

	public static void main(String[] args) {
		/**
		 * WSDL
		 * -<service name="HelloServiceService">
		 */
		HelloServiceService hss = new HelloServiceService();
		/**
		 * WSDL
		 * -<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
		 */
		HelloService hs = hss.getHelloServicePort();
		
		String ret = hs.sayHello("jack:");
		System.out.println(ret);
		System.out.println(hs.getClass().getName());
		
		
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值