Web Service

1.什么是WebSevice

WebService:一种基于Web的服务,服务端提供一些资源让客户端进行访问,获取一些公共的数据。比如天气预报,股票信息,手机归属地查询等等。
WebServcie是一种跨平台,跨语言的通信规范。

气象局负责将收集的每天的天气信息,并将这些信息暴露出来,各大站点的应用去调用题目得到当天或者未来几天的天气情况,然后以不同形式展示出来。
网站提供了天气预报的信息,其实网站本身什么都没有做,只是调用了一下远程接口。这个就是WebService的典型应用。

1.Web服务的几个重要术语

WSDL(Web Services Description Language):Web服务描述语言。一个XML文档,WSDL定义了Web Service 的名称、处理服务的方法、请求的参数及返回的数据格式。一个WebService对应唯一一个wsdl文档。
SOAP(Simple Object Access Protocol):简单对象传输协议。一个基于HTTP和XML的协议,用于在Web上的应用程序交换结构化的数据。
SEI(WebService Endpoint Interface):WebService服务端用来处理请求的接口。
CXF(Celtix + XFire):一个Apache的用于开发webService服务器和客户端框架。

JAX-WS方式开发WebService

开发步骤:
创建一个JavaProject项目
添加WebService的接口,使用@WebService和@WebMethod注解修饰类。
添加实现WebService接口的类,使用@WebService修饰,并指定EndpointInterface属性到接口的完整类名。
添加一个Endpoint来发布WebService。

代码示例:

WebService接口:

//添加JavaProject,JaxServer
//添加WeatherService接口,使用@WebService修饰类,使用@WebMethoid修饰方法。
@WebService
public interface Text {
	@WebMethod
	public List<String> getProvince();

	@WebMethod
	public List<String> getCitys(String proe);

}

WebService实现类:

//添加WeatherServiceImpl类,实现WeatherService接口,使用@WebService修饰类,
//添加endpointInterface属性,并重写接口的方法。
@WebService(endpointInterface = "com.znsd.service.Text")
public class Textimp implements Text {
 		private static List<String> list;
 		static {
 			list=new ArrayList<String>();
 		}
 		@Override
	public List<String> getProvince() {
		list.add("中国");
		list.add("美国");
		list.add("印度");
		return list;
	}
 		@Override
	public List<String> getCitys(String proe) {
 		List<String> list2 =new ArrayList<String>();
 		switch (proe) {
		case "中国":
			list2.add("中国好啊");
			break;
		case "美国":
			list2.add("垃圾美国");
			break;
		case "印度":
			list2.add("印度老白干,喝出死人味");
			break;
		default:
			list2.add("没有这个东西");
			break;
		}
		return list2;
	}

}

发布WebService:

//使用Endpoint.publish()发布WebSevice,然后通过wsdl访问webservice。
//使用http://localhost:9999/weather/getweathers?wsdl访问webService服务器地址。
public static void main(String[] args) {
		String address="http://localhost:9999/weather/getweathers";
		Endpoint.publish(address,new Textimp());
		System.out.println("发布成功");
	}

WebService常用注解

@WebService(必须的)
修饰一个类或者接口为一个WebSevice,如果有接口,必须提供endpointInterface指定接口名称,否则会报警告。
@WebMethod(可省略)
修饰一个方法为一个Web方法。可以被远程调用。也可以通过exclude=true来排除某个方法。
@WebParm(可省略)
修饰生成wsdl文件中方法的参数名称。

使用JAX-WS调用服务

创建客户端项目,在src目录下生成调用代码
使用wsimport工具从WSDL文件生成客户端调用所需的Java类:
在这里插入图片描述
-p:-将生成的类,放于指定的包下。
-keep:服务端发布的地址(必须要在后面加上?wsdl)

生成的包:

在这里插入图片描述

启动客户端测试:

在这里插入图片描述

调用.net编写的WebService

进 入 web服务(http://www.webxml.com.cn/zh_cn/web_services.aspx)里下载天气的WSDL
在这里插入图片描述

在这里插入图片描述

生成本地java文件后,可以在本地调用class方法了。

public static void main(String[] args) {
	WeatherWSSoap wa=new  WeatherWS().getWeatherWSSoap();
	ArrayOfString weather = wa.getWeather("深圳",null);
	System.out.println(weather.getString());
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值