webService简介
来源:互联网 发布:卡特生涯数据 编辑:程序博客网 时间:2024/05/17 07:58
webService简介
一、WebService概念
1、如何调用另外一台机器上的java程序
客户端:-->调用个service的哪个方法,传参数
helloService.sayHello#name#666
2、使用中的WebService实现---使用@WebService及Endpoint ,,Java1.6_20以后的版本
A、发布一个WebService
@WebService
public class HelloWorld {
public String sayHello(String name){
System.out.println(name+"调了SayHello!");
return name+"早上好,现在时间是:"+new Date();
}
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/hello", new HelloWorld());
System.out.println("服务器已启动!");
}
B、查看wsdl结果--服务描述语言,描述我们所发布的服务的详细信息
http://localhost:9999/hello?wsdl
C、wsimport 生成Java客户端调用WebService的源代码(只需要一个wsdl文件即可)
wsimport -s . http://localhost:9999/hello?wsdl
D、在java客户端调用WebService
public static void main(String[] args) {
//通过HelloWorldService来获得一个连接到HelloWorld这个Web服务的实例
HelloWorld hello= new HelloWorldService().getHelloWorldPort();
String ret= hello.sayHello("test");
System.out.println("结果:"+ret);
}
3、基于Java1.6_20以前的版本来发布WebService
/**
* 基于JDK1.6_20以前的版本,在标注WebService的时候,写的注解更加详尽一点
* @author Administrator
*
*/
@WebService(targetNamespace="http://localhost:999/myservice")
@SOAPBinding(style=Style.RPC)//只支持RPC的消息风格
public class Service2 {
@WebMethod
public String test(String msg) {
System.out.println(msg);
return msg + " form the Server!";
}
}
4、小结
WebService叫Web服务--基于Web(htttp)协议提供的一种服务。是可以跨平台跨语言调用。他会以一个URI暴露出去。
广义的WebService,所有在Web服务器中运行的应用,只要能通过一个uri来访问,都可以认为是一个WebService.
标准的WebService,是使用WSDL语言来描述调用的接口信息,基于SOAP协议(基于XMl协议)来传递调用的信息的一种Web应用.
xml+wsdl+soap
学习websrvice的内容
A、webService的服务器端如何编程实现.
(1)、直接JAVA中自带的Endpoint.publish()来发布;
(2)、在web应用中嵌入webservice服务器(其实是web服务器中的一个应用,专门用来处理webservice请求的)。
B、如何去调用别人的WebService
(1)、直接使用wsimport来生成java的客户端.
(2)、自己写程序发送http请求,发送符合SOAP协议的请求体去调用webService。
(3)、利用等三方框架
二、WebService调用通信细节
1、WebService调的过程
A、第一次请求
GET /hello?wsdl HTTP/1.1
User-Agent: Java/1.6.0_10
Host: localhost
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
B、第一次请求的响应
HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml;charset="utf-8"
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://itcast.gz/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://itcast.gz/" name="HelloWorldService">
<types>
<xsd:schema>
<xsd:import namespace="http://itcast.gz/" schemaLocation="http://localhost/hello?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"></part>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"></part>
</message>
<portType name="HelloWorld">
<operation name="sayHello">
<input message="tns:sayHello"></input>
<output message="tns:sayHelloResponse"></output>
</operation>
</portType>
<binding name="HelloWorldPortBinding" type="tns:HelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="sayHello">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="HelloWorldService">
<port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">
<soap:address location="http://localhost/hello"></soap:address>
</port>
</service>
</definitions>
C、第二次请求
发送的是POST请求,并且有请求体,请求体中的内容Content-Type是text/xml; charset=utf-8。请求体是一个基于SOAP协议的确xml文档。
POST /hello HTTP/1.1
SOAPAction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Type: text/xml; charset=utf-8
User-Agent: Java/1.6.0_10
Host: localhost
Connection: keep-alive
Content-Length: 194
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHello xmlns:ns2="http://itcast.gz/"><arg0>itcast</arg0></ns2:sayHello></S:Body></S:Envelope>
D、第二次响应,响应的也是xml文件,XMl文件的内容就是调用结果,是基于SOAP协议封装的xml文档。
HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml;charset="utf-8"
5e
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body>
b2
<ns2:sayHelloResponse xmlns:ns2="http://itcast.gz/"><return>itcast早上好,现在时间是:Sun Jan 15 14:35:33 CST 2012</return></ns2:sayHelloResponse></S:Body></S:Envelope>
三、熟悉wsdl
三、熟悉wsdl
1、wsdl语法简介
2、webservice注解
WebService的注解包括:
@WebService-定义服务--定义服务的总体信息,比如有哪些protType,服务的名称,服务的类型等等。
@WebMethod-定义方法---定义方法的名称以及是否作为webService发布出去
@WebResult-定义返回值--
@WebParam-定义参数--定义方法调用参数的名称等信息。
3、WebService客户端代码简化
public class ClientTest {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
URL url = new URL("http://localhost:9999/hello?wsdl");
//根据服务器端wsdl文件,创建一个客户端Service组件,相当于引用服务器webservice
Service service= Service.create(url, new QName("http://itcast.gz/", "HelloWorld2Service"));
//getPort是从WebService服务中获得一个组件(port)
HelloWorld2 hs= service.getPort(new QName("http://itcast.gz/", "HelloWorld2Port"), HelloWorld2.class);
//运行即可
String ret=hs.sayHello("ddd");
System.out.println("返回的结果:"+ret);
}
@WebService(name = "HelloWorld2", targetNamespace = "http://sanfy.cn/")
@XmlSeeAlso({
})
public interface HelloWorld2 {
/**
*
* @param arg0
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "sayHello", targetNamespace = "http://sanfy.cn/", className = "cn.sanfy.SayHello")
@ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://sanfy.cn/", className = "cn.sanfy.SayHelloResponse")
public String sayHello(
@WebParam(name = "arg0", targetNamespace = "")
String arg0);
}
四、SOAP协议简介
1编程访问WebService
public static void main(String[] args) throws Exception {
URL url=new URL("http://localhost:9999/hello");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
//设置请求头信息
conn.setRequestProperty("SOAPAction:", "");
conn.setRequestProperty("Accept", "text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
OutputStream out= conn.getOutputStream();
out.write("<?xml version=\"1.0\" ?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:listUsers xmlns:ns2=\"http://baidu.com\"/></S:Body></S:Envelope>".getBytes());
out.close();
System.out.println("调用得到的结果:");
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line=null;
while((line=br.readLine())!=null){
System.out.println(line+"\r\n");
}
br.close();
}
- webservice 简介
- WebService简介
- webservice简介
- WebService简介
- WebService简介
- webservice简介
- webservice简介
- WEBSERVICE简介
- webservice简介
- WebService简介
- webservice简介
- webservice 简介
- webService简介
- WebService简介
- WebService简介
- webservice简介
- webService简介
- WebService简介
- 第一次遇到asynch descriptor resize等待事件
- JAVA开发人员学习C++之变量名和变量地址之间是什么关系
- mysql优化Analyze Table
- VirtualBox网卡设置
- nfs
- webService简介
- JAVA发送邮件
- Matlab使用subplot合并子图
- Activity跳转短暂黑屏
- 巧妙写工具类
- 微信开发 该公众号暂时无法提供服务,请稍候再试 解决办法
- matlab opencv混合编程(“mex找不到指定模块”)
- 网站优化更需求法则性任务
- 51单片机(三)——定时器/计数器