一、Webservice简介
是一种跨编程语言和跨操作系统平台的运程调用技术。
基本概念:
webservice也叫XML Web Service是一种可以接收从Internet或者Internet上的其他系统汇总传递过来的请求,轻量级的独立的通讯技术。
平台元素:
XML:(Extensible Markup Language)扩展型可标记语言。面向短期的临时数据处理、面向万维网络,是Soap的基础。
Soap:(Simple Object Access Protocol)简单对象存取协议。是XML Web Service 的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,它可以支持不同的底层接口,像HTTP(S)或者SMTP。
WSDL:(Web Services Description Language ---- web Service描述语言) WSDL 文件是一个 XML 文档,用于说明一组 SOAP 消息以及如何交换这些消息。大多数情况下由软件自动生成和使用。
UDDI:(Universal Description, Discovery, and Integration 一种目录服务) 是一个主要针对Web服务供应商和使用者的新项目。UDDI利用SOAP消息机制(标准的XML/HTTP)来发布,编辑,浏览以及查找注册信息。它采用XML格式来封装各种不同类型的数据,并且发送到注册中心或者由注册中心来返回需要的数据。
目的:
可在不同的应用程序与平台之间交换数据
二、jdk开发WebService
1、服务端实现
定义一个interfac,使用@WebService注解标注接口,@WebMethod注解标注方法
package sercvice;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface IScore {
@WebMethod
public Integer find(String name);
}
定义此接口的实现类,并使用@WebService注解标注
package sercvice;
import java.util.Random;
import javax.jws.WebService;
@WebService
public class ScoreImpl implements IScore{
Random randDom = new Random();
public Integer find(String name) {
return randDom.nextInt(150);
}
}
测试 :使用Endpoint(终端)类发布webservice
package sercvice;
import javax.xml.ws.Endpoint;
public class Demo {
public static void main(String[] args) {
String address = "http://localhost:8088/myscore";
Endpoint.publish(address, new ScoreImpl());
System.out.println("服务启动==============");
}
}
测试服务
直接在浏览器中输入: http://localhost:8080/myscore?wsdl
2、客户端实现
使用jdk的wsimport.exe(java_home\bin)工具生成客户端代码
徽标键+R 进入在eclipse中项目的src目录,将
wsimport -keep http://localhost:8088/myscore?wsdl 或
wsimport -keep *.xml
填入回车后会生成文件
写一个类
package test;
import sercvice.ScoreImpl;
import sercvice.ScoreImplService;
public class FindScore {
public Integer find(String name) {
// 创建工厂对象
ScoreImplService factory = new ScoreImplService();
// 通过工厂对象创建WebServiceImpl对象
ScoreImpl scoreImpl = factory.getScoreImplPort();
Integer find = scoreImpl.find(name);
return find;
}
}
测试:
package test;
public class Test {
public static void main(String[] args) {
String name = "22";
FindScore find = new FindScore();
Integer find2 = find.find(name);
System.out.println(name+" = "+find2);
}
}
注意测试时要将服务端启动
用webservice查询天气
进入有右键另存为,将下载的xml文件随意的放到一个地方,如E盘
打开命令工具到项目目录src下填入:wsimport -keep E:\WeatherWS.asmx.xml
会报错:
15行、61行、101行有错误
Exception in thread "main" com.sun.tools.internal.ws.wscompile.AbortException
解决:修改下载的xml文件,将所有的<s:element ref="s:schema" /> <s:any />改成 <s:any minOccurs="2" maxOccurs="2"/>,一共有三处需要修改,建议查找<s:element ref="s:schema" />,修改时把<s:any />也要删掉。
打开命令工具(cmd)到项目目录src下填入:wsimport -keep E:\WeatherWS.asmx.xml
解析之后src目录下会生成许多文件,刷新项目会有
测试:
package test;
import java.util.List;
import cn.com.webxml.ArrayOfString;
import cn.com.webxml.WeatherWS;
import cn.com.webxml.WeatherWSSoap;
public class Test {
public static void main(String[] args) {
String theCityCode = "长沙";
WeatherWS weatherWS = new WeatherWS();
WeatherWSSoap weatherWSSoap = weatherWS.getWeatherWSSoap();
//后面的userID,若有的网站已经付过钱,则可直接访问
//调用webservice提供的getWeather方法获取上海的天气预报情况
ArrayOfString weather = weatherWSSoap.getWeather(theCityCode, null);
List<String> string = weather.getString();
//遍历天气预报信息
for (String str : string) {
System.out.println(str);
System.out.println("===========");
}
/*//获得中国省份、直辖市、地区和与之对应的id
ArrayOfString regionProvince = weatherWSSoap.getRegionProvince();
for (String s : string) {
System.out.println(s);
System.out.println("--------------");
}*/
}
}