简单WebService入门

本文介绍了Web Services的概念,它是独立、自我描述的应用程序组件,使用XML和HTTP进行通信。核心组件包括SOAP(用于信息交换)、UDDI(用于服务发现)和WSDL(用于描述服务)。文章还演示了如何创建服务端,通过Endpoint发布服务,并在客户端使用wsimport命令生成对应的WebService文件以消费服务。
摘要由CSDN通过智能技术生成

什么是Web Services

Web Services 是应用程序组件,使用开放协议进行通信,是独立的(self-contained)并可自我描述的,可通过使用UDDI来发现,可被其他应用程序使用,XML 是 Web Services 的基础。

基础的 Web Services 平台是 XML + HTTP。
HTTP 协议是最常用的因特网协议。
XML 提供了一种可用于不同的平台和编程语言之间的语言。

Web services 平台的元素:

SOAP (简易对象访问协议)
UDDI (通用描述、发现及整合)
WSDL (Web services 描述语言)

SOAP 是一种使应用程序有能力通过 HTTP 交换信息的基于 XML 的简易协议。
或者可以更简单地说:SOAP 是一种用于访问 web service 的协议。

WSDL 是基于 XML 的用来描述 Web services 以及如何访问它们的一种语言。
WSDL 可描述 web service,连同用于 web service 的消息格式和协议的细节。

创建服务端,使用Endpoint来发布服务:

//要发布的服务接口
public interface HelloService {
	String sayHello(String name);
}
@WebService
public class HelloServiceImpl implements HelloService {
	@WebMethod
	public String sayHello(String name) {
		return "Hello," + name +"java是世界上最好的语言";
	}
}
public class App {

	public static void main(String[] args) {
		Endpoint.publish("http://127.0.0.1:2121/web_service", new HelloServiceImpl());
		System.out.println("webService发布成功........");
	}
}

根据发布的URL在后面加上**?wsdl**在浏览器中输入 http://127.0.0.1:2121/web_service?wsdl

<!--
 Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.api.demo.aloys.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.api.demo.aloys.com/" name="HelloServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://impl.api.demo.aloys.com/" schemaLocation="http://127.0.0.1:2121/web_service?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="HelloServiceImpl">
<operation name="sayHello">
<input wsam:Action="http://impl.api.demo.aloys.com/HelloServiceImpl/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://impl.api.demo.aloys.com/HelloServiceImpl/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloServiceImplPortBinding" type="tns:HelloServiceImpl">
<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="HelloServiceImplService">
<port name="HelloServiceImplPort" binding="tns:HelloServiceImplPortBinding">
<soap:address location="http://127.0.0.1:2121/web_service"/>
</port>
</service>
</definitions>

创建一个客户端(一个普通的java项目即可):
打开cmd输入命令:
wsimport -s 客户端项目的src路径 -keep http://127.0.0.1:2121/web_service?wsdl

刷新客户端项目,会有对应的WebService生成的文件,然后调用即可消费服务:

public class App {
	public static void main(String[] args) {
		HelloServiceImpl service = new HelloServiceImplService().getHelloServiceImplPort();
		System.out.println(service.sayHello("jack "));
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值