学习概述:
以前一直以为WebService是一个很强很大很难学的东西,今天有空学习了一下,其实也是just so so ,可能是我学的刚入门,反正入门我感觉还不是太难的,直接从例子入手是学习WebService的一个不错的方法!
下面开始说,步骤:
1,首先要明白什么是WebService
1),因为我是学java的,java是一种跨平台的语言,那么WebService 就是一种 跨语言的一种技术。可以在用户没有感觉的情况下直接访问其他工程项目就像访问自己的项目一样,这里的工程项目,不单单包括java的项目,也包括比如说c,c#。.net等语言;
2),WebService拥有自己飞访问协议,SOAP协议,simple Object Access protocol 简单对象访问协议
3),简单的说: SOAP协议 = http协议 + xml(请求和响应接收的都是xml文件)
4),JAX-WS java Api XML baseed WebService 基于 xml的WebService 。请求或是响应接收的都是xml文件
JAX-RS java Api XML base RESTfull webService REST -静态地址转换,转换成参数
如:http://loccalhost:8080/project/save/jack/89/BJ
http://localhost:8080/project/save?name=Jack&age=89&addr=BJ
2,webService相关术语:
XML – webServic是指web的一个服务,也可以是发布网上的一段代码。XML文件是webService的传递数据,说明数据,调用时起到一个说明作用。
其中之一:
WSDL :
*.wsdl – 就是一个xml,它是webservice的调用说明书。
一般可以从网上获取:http://?wsdl
Namespace:
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/ soap 命名空间的名称
xmlns= "http://schemas.xmlsoap.org/wsdl/soap/没有命名空间就是默认
3, 只要有了wsdl就可以根据wsdl生成wsdl相对应的webService本地代码
JDK 1.6以后自带了根据wsdl生成本地代码的工具,
wsimport
例如 :C:\a>wsimport -s . -d . http://192.168.17.39:8080/hello?wsdl
其中: -s 是生成源码文件,-d是生成class文件(默认生成,可以省略) ,http://192.168.17.39:8080/hello?wsdl 就是你的wsdl访问地址
4, 下面我们就简单来开发一个webService
要求:jdk1.6.0.20版本以上
第一步:创建一个工程项目,这个就不用说了
第二步:创建一个类,其中必须有一个public公开的方法,这里的一个方法其实就相当于webService的一个服务,这个方法必须是静态的
@WebService
public class HelloWs {
public String sayhi(String str){
System.out.println("str = " + str);
return "hello = " + str ;
}
/*
* 右键run As,对这个类进行对外发布
* */
public static void main(String[] args) {
//当执行了这个类的时候,程序会开一个线程等待,这个项目不会停止,因为它在等待其他工程访问它
Endpoint.publish("http://192.168.17.39:8080/hello", new HelloWs());
System.out.println("发布成功");
}
}
第三步:获取WSDL 说明书
上面的代码中:Endpoint.publish 意思是发布了一个服务器类,其中的 http://192.168.17.39:8080/hello 然后再后面加上 ?wsdl 就是WSDL的地址
第四步:生成本地代码:
cmd 命令行中输入 C:\a>wsimport -s . -d . http://192.168.17.39:8080/hello?wsdl
然后去c盘下a文件夹下查看生成的文件
把class文件可以删掉,然后把java文件,带包一起拷贝到另一个项目中
第五步:在你新建的那个项目中新建一个测试类:
@Test
public void test(){
HelloWs helloWs = new HelloWsService().getHelloWsPort();
String str = helloWs.sayhi("祥子");
System.out.println("str: " + str);
}
你会看到控制台会打出:str: hello =祥子
切换到服务器的控制台页面你会看到打出:str = 祥子
到此,一个简单的webService就OK了,有没有很简单呢?
5,用ajax调用webService 的方法:
这里需要注意几点:
1,用ajax调用webService不能用jquery,因为jquery不支持跨域,需要使用原生的javascript
2,ajax调用webService发送和响应的都是xml数据
xm数据必须l包括:Envloper ,Body ,Head – 可选的
3,因为webService基于的是SOAP协议,因为SOAP协议只支持post提交方式,所以只能用post方式提交
4,SOAP1.1请求类型 : Content-Type=text/xml;charset=UTF-8
5,JDK默认支持SOAP1.1,暂时不说SOAP1.2
下面是ajax调用webService页面:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<title>ws.html</title>
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<input type="text" name="name" id="name">
<br/>
<input type="button" id="btn" value="GO">
</body>
<script type="text/javascript">
$(function(){
var xhr = new ActiveXObject("Microsoft.XMLHttp");
$("#btn").click(function(){
var nm = $("#name").val();
alert(nm);
var uri = "http://192.168.17.39:8080/hello";
xhr.open("POST",uri,true);
xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200){
var xx = xhr.responseXML;
xx = $(xx);
var str = xx.find("return").text();
alert(str);
}
}
};
var xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"'+
' xmlns:q0="http://ws.zfx.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" '+
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<soapenv:Body><q0:sayhi ><arg0>'+nm+'</arg0></q0:sayhi></soapenv:Body></soapenv:Envelope>';
xhr.send(xml);
});
})
</script>
</html>
页面效果:
大家可以看到 xhr.send(xml); 是xml文本,但是xml文本是从哪来的呢? 其实是 WSDL的一部分,但是Eclipse和Myeclipse 提供了一个工具,可以生成SOAP协议的文本内容
步骤如下:
第二步:输入webservice的wsdl的地址
第四步:点击 GO 之后会在下面的控制栏中看到
第五步:点击 Source
上面那个就是用来像服务器端发送的xm文本文件,把它粘贴到ajax调用webService 的js中取即可。
调用add方法添加一个user对象所发送的xml文本
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.zfx.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <q0:add>
- <arg0>
<name>祥子</name>
</arg0>
</q0:add>
</soapenv:Body>
</soapenv:Envelope>
6,根据jax-ws文档开发一个标准的webService
以下创建一个接口和接口的实现类:
接口:
import javax.jws.WebService;
@WebService
public interface ICalculate {
public int computer(int a,int b);
}
实现类:
@WebService
public class Calculate implements ICalculate{
@Override
public int computer(int a, int b) {
System.err.println("a:"+a+",b:"+b);
return a+b;
}
}
发布服务的类:
import javax.xml.ws.Endpoint;
public class Server01 {
public static void main(String[] args) throws Exception {
Endpoint end = Endpoint.publish("http://192.168.1.100:8080/calculate",new Calculate());
System.err.println("发布成功了..");
}
}
调用方法:
1,wsimport生成的java文件拷贝到另一个项目中
2,ajax调用webServicce
3,自己写代码调用webService。
要求,必须要拥有共同使用的接口:
第一步:生成本地代码。但只需要生成的那个接口
@Test
public void handInvoke() throws Exception{
String urlString = "http://192.168.1.100:8080/calculate?wsdl";
String nameSpace = "http://jaxws.itcast.cn/";
//获取Service对象。即相当于new CalculateService();
Service service =
Service.create(new URL(urlString),
new QName(nameSpace,"CalculateService"));
//获取接口的远程对象
Calculate cl= service.getPort(new QName(nameSpace,"CalculatePort"),
Calculate.class);
//调用测试
int a = cl.computer(9, 10);
System.err.println(">>>:"+a);
}
7,对某些方法公开对某些方法不公开@WebMethod
/*
* 不对外公布,反之公布
* */
@WebMethod(exclude=true)
public String add(User user){
userList.add(user);
return "添加 :" +user.getName()+ ",用户成功";
}
8,@WebService(targetNamespace="http://zfx.com")
publicclass Calculateimplements ICalculate{
9,更多属性
,
10,通过注解修改返回值
11,如果webService调用一个服务,返回的是一个List对象格式,那么会返回
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
- <S:Body>
- <ns2:allResponse xmlns:ns2="http://ws.zfx.cn/">
- <return>
<age>666</age>
<name>Jack</name>
</return>
- <return>
<age>777</age>
<name>张三</name>
</return>
</ns2:allResponse>
</S:Body>
</S:Envelope>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
- <S:Body>
- <ns2:allResponse xmlns:ns2="http://ws.zfx.cn/">
- <return>
[{“name”:”jack”,”age”:90},{“name”:张三,”age”:88}]
</return>
</ns2:allResponse>
</S:Body>
</S:Envelope>
ajax 解析:
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200){
var xml = xhr.responseXML;
xml = $(xml);
var str = xml.find("return").text();
str = eval('('+str+')');
for(var i in str){
alert(str[i].name+","+str[i].age);
}