WebService,顾名思义就是基于Web的服务。它使用Web(HTTP)方式,接收和响应外部系统的某种请求。从而实现远程调用.
1:从WebService的工作模式上理解的话,它跟普通的Web程序(比如ASP、JSP等)并没有本质的区别,都是基于HTTP传输协议的程序。
2:WebService所使用的数据均是基于XML格式的。目前标准的WebService在数据格式上主要采用SOAP协议。SOAP协议实际上就是一种基于XML编码规范的文本协议。
名词1:WSDL – WebService Description Language – Web服务描述语言。
•通过XML形式说明服务在什么地方-地址。
•通过XML形式说明服务提供什么样的方法 – 如何调用。
名词2:SOAP-Simple Object Access Protocol(简单对象访问协议)
•SOAP作为一个基于XML语言的协议用于有网上传输数据。
•SOAP = 在HTTP的基础上+XML数据。
•SOAP是基于HTTP的。
•SOAP的组成如下:
•Envelope – 必须的部分。以XML的根元素出现。
•Headers – 可选的。
•Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。
在Java项目中发布第一个WS服务:
在JDK1.6中JAX-WS规范定义了如何发布一个webService服务。
JAX-WS是指JavaApi for XML –WebService.
1.用Jdk1.6.0_21以后的版本发布一个WebService服务.
2.与Web服务相关的类,都位于javax.jws.*包中。
主要类有:
1.@WebService- 它是一个注解,用在类上指定将此类发布成一个ws.
2.Endpoint – 此类为端点服务类,它的方法publish用于将一个已经添加了@WebService注解对象绑定到一个地 址的端口上。
/**
* 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
*
*/
@WebService(targetNamespace="http://www.usst.cn",serviceName="MyService")
public class HelloService {
@WebMethod(operationName="hello")
@WebResult(name="return")
public String sayHello(
@WebParam(name="name")
String name,
@WebParam(name="age")
int age){
System.out.println("sayHello called...");
return "hello " + name;
}
@WebMethod(exclude=true)
public String sayHello2(String name){
System.out.println("sayHello called...");
return "hello " + name;
}
public static void main(String[] args) {
//参数1:绑定服务的地址 参数2:提供服务的实例
Endpoint.publish("http://192.168.1.101:5678/hello", new HelloService());
System.out.println("server ready...");
}
}
•static Endpoint.publish(String address, Object implementor)
在给定地址处针对指定的实现者对象创建并发布端点。
在给定地址处针对指定的实现者对象创建并发布端点。
•stop方法用于停止服务。
•EndPoint发布完成服务以后,将会独立的线程运行。所以,publish之后的代码,可以正常执行。
其他注意事项:
•给类添加上@WebService注解后,类中所有的非静态方法都将会对外公布。
•不支持静态方法,final方法。-
•如果希望某个方法(非static,非final)不对外公开,可以在方法上添加@WebMethod(exclude=true),阻止对外公开。
•如果一个类上,被添加了@WebService注解,则必须此类至少有一个可以公开的方法,否则将会启动失败。
客户端调用WebService的方式
l
通过
wsimport
生成客户端代码
wsimport -s . http://192.168.1.101:5678/hello
把生成的包及源码拷贝到工程中,通过以下代码调用WebService:
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 soap = hss.getHelloServicePort();
String str = soap.sayHello("zhangsan");
System.out.println(str);
System.out.println(soap.getClass().getName());
}
}
l
通过
ajax
调用
(
js+XML
)
<html>
<head>
<title>通过ajax调用webservice服务</title>
<script>
var xhr;
function sendAjaxWS(){
xhr = new ActiveXObject("Microsoft.XMLHTTP");
//指定ws的请求地址
var wsUrl = "http://10.10.49.16:5678/hello";
//手动构造请求体
var requestBody = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' +
' xmlns:q0="http://service.usst.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema "'+
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<soapenv:Body><q0:sayHello><arg0>'+document.getElementById("msg").value+'</arg0> <arg1>10</arg1> </q0:sayHello></soapenv:Body></soapenv:Envelope>';
//打开连接
xhr.open("POST",wsUrl,true);
//重新设置请求头
xhr.setRequestHeader("content-type","text/xml;charset=utf8");
//设置回调函数
xhr.onreadystatechange = _back;
//发送请求
xhr.send(requestBody);
}
//定义回调函数
function _back(){
if(xhr.readyState == 4){
if(xhr.status == 200){
var ret = xhr.responseXML;
//解析xml
var eles = ret.getElementsByTagName("return")[0];
alert(eles.text);
}
}
}
</script>
</head>
<body>
<input type="text" id="msg" />
<input type="button" οnclick="sendAjaxWS();" value="通过ajax调用webservice服务"/>
</body>
</html>
l
通过
URLConnection
调用
public class App {
public static void main(String[] args) throws Exception {
// 指定webservice服务的请求地址
String wsUrl = "http://192.168.1.108:5678/hello";
URL url = new URL(wsUrl);
URLConnection conn = url.openConnection();
HttpURLConnection con = (HttpURLConnection) conn;
// 设置请求方式
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("content-type", "text/xml;charset=UTF-8");
// 手动构造请求体
String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
+ " xmlns:q0=\"http://service.usst.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema \" "
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ "<soapenv:Body><q0:sayHello><arg0>lisi</arg0> <arg1>10</arg1> </q0:sayHello></soapenv:Body></soapenv:Envelope>";
//获得输出流
OutputStream out = con.getOutputStream();
out.write(requestBody.getBytes());
out.close();
int code = con.getResponseCode();
if(code == 200){//服务端返回正常
InputStream is = con.getInputStream();
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
int len = 0;
while((len = is.read(b)) != -1){
String str = new String(b,0,len,"UTF-8");
sb.append(str);
}
System.out.println(sb.toString());
is.close();
}
con.disconnect();
}
}
l通过客户端编程的方式调用
通过wsimport -s -p cn.usst.service . http://192.168.1.101:5678/hello导入接口类
public class App {
public static void main(String[] args) throws Exception {
Service s = Service.create(new URL("http://192.168.1.101:5678/hello?wsdl"), new QName("http://service.usst.cn/", "HelloServiceService"));
HelloService hs = s.getPort(new QName("http://service.usst.cn/","HelloServicePort"), HelloService.class);
String str = hs.sayHello("lisi", 10);
System.out.println(str);
System.out.println(hs.getClass().getSimpleName());
}
}