目录
1 WebService 介绍
Web service 是一个平台独立的,低耦合的 web 的应用程序用于开发分布式的互操作的应用程序。Web Service 技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。
SOAP(Simple Object Access Protocol)
简单对象访问协议
:
SOAP=http+xml
WSDL(Web Services Description Language) Web Service
的描述语言
:
一个
webservice
服务的说明书
,
通过该说明书可以完成
webservice
服务的调用
2 服务提供者实现
2.1 创建 ws-provider 项目
2.2 创建 UserService 接口
package com.zgl.service;
import javax.jws.WebService;
/***
* 需要发布的服务对应的接口
* @author Administrator
* @WebService 发布远程的 web 服务,默认情况下该接口中的
* 所有 public 方法都会被发布
*/
@WebService
public interface UserService {
//@WebMethod()
public String sayHello(String name);
}
2.3 创建 UserServiceImpl 实现类
package com.zgl.service.impl;
import javax.jws.WebService;
import com.zgl.service.UserService;
@WebService
public class UserServiceImpl implements UserService {
@Override public String sayHello(String name)
{ // TODO Auto-generated method stub return "hello "+name;
}
}
2.4 发布 webservice 服务
package com.zgl.app;
import javax.xml.ws.Endpoint;
import com.zgl.service.UserService;
import com.zgl.service.impl.UserServiceImpl;
public class WsProviderApp {
public static void main(String[] args) {
/***
* 完成 webservice 服务的发布
*/
//发布的 webservice 服务的访问地址
String address="http://localhost:9999/ws";
//创建 UserService 对象
UserService userService=new UserServiceImpl();
//发布具体的 webservice 服务
Endpoint.publish(address, userService);System.out.println("----------发布 webservice 服务-------------");
}
}
2.5 发布服务
3 获取 wdsl 文档
3.1 文档内容
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<definitions targetNamespace="http://impl.service.bjsxt.com/" name="UserServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://impl.service.bjsxt.com/" schemaLocation="http://localhost:9999/ws?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="UserServiceImpl">
<operation name="sayHello">
<input wsam:Action="http://impl.service.bjsxt.com/UserServiceImpl/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://impl.service.bjsxt.com/UserServiceImpl/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="UserServiceImplPortBinding" type="tns:UserServiceImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="UserServiceImplService">
<port name="UserServiceImplPort" binding="tns:UserServiceImplPortBinding">
<soap:address location="http://localhost:9999/ws"/>
</port>
</service>
</definitions>
3.2 文档内容从下往上看
3.3 可获取的信息
4 服务消费者实现
4.1 创建 ws-consumer 项目
4.2 wsimport 生成消费者代码
4.3 生成的文件
4.4 消费远程服务
package com.zgl.app;
import com.zgl.client.UserServiceImpl;
import com.zgl.client.UserServiceImplService;public class WsConsumerApp {
public static void main(String[] args) {
/***
* 完成 webservice 服务的消费
*/
//创建服务类对象
UserServiceImplService service=new UserServiceImplService();
//获得远程服务的代理对象
UserServiceImpl userService=service.getUserServiceImplPort();
System.out.println(userService.getClass().getName());
//进行远程服务调用
String result = userService.sayHello("张三");
System.out.println("result="+result);
}
}
5 WEBSERVICE 相关 API 总结
@WebService
:指定发布远程的服务
,
哪些功能发布为远程服务
EndPoint:
发布具体的远程服务
,
给提供远程服务的对象绑定一个
url