微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

教你简单玩转WebService -JAX-WS

WebService 是一种跨系统的调用的方式,他的特点有以下几点

1.跨系统

2.通过xml进行交流

3.通过http传输(其实底层是socket)

现在我给大家介绍jAX-WS 一种简单的webservice  

JAX-WS (JAVA API  XML  WEB SERVICE )

步骤如下

1.建立一个java 工程(注意这个工程jdk 版本必须是,1.6.021++),导入@WebService 注解

用Endpoint的静态方法发布这个WEBSERVICE  这里有两个参数

一个参数是发布的地址 一般指的是本机

第一参数是发布的事例,这里是本类的对象

@WebService
public class HelloService {

public String sayHello(String name)
{
System.out.println("hello!");
return name+"22";
}
@WebMethod(exclude=true)
public String sayHello2(String name)
{  return name+"33";}
/**
* @param args
*/
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:6789/hello",new HelloService());
}
}

2.将发布的webService的url 加上?wsdl  拷贝到浏览器

<!--
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 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://service.test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.test.com/"name="HelloServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.test.com/" schemaLocation="http://127.0.0.1:6789/hello?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="HelloService">
<operation name="sayHello">
<input wsam:Action="http://service.test.com/HelloService/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://service.test.com/HelloService/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloServicePortBinding" type="tns:HelloService">
<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="HelloServiceService">
<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
<soap:address location="http://127.0.0.1:6789/hello"/>
</port>
</service>
</deFinitions>

这里的<soap:address location="http://127.0.0.1:6789/hello"/> 指的是调用地址

service  name 指的是服务的名字

operation name="sayHello 指的是服务的方法

<service name="HelloServiceService">  
<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
<soap:address location="http://127.0.0.1:6789/hello"/>
</port>
</service>
<portType name="HelloService">
<operation name="sayHello">
<input wsam:Action="http://service.test.com/HelloService/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://service.test.com/HelloService/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
3.生成客户端

   前提,装了java ,并且是jdk.1.6.021版本以上,及配了环境变量

 在命令行里面用如下命令 ,将会在 E:\webservice 生成文件及源码

   E:\webservice> wsimport  -s .   http://127.0.0.1:6789/hello?wsdl

4.使用客户端

生成的客户端拷贝到新的项目,

调用如下

1.首先先通过webService的服务名 建立一个对象实例

2.通过该实例helloServiceService.getHelloServicePort() 找到绑定的类HelloService的代理

3.调用代理的方法即可

public class App { /** * @param args */ public static void main(String[] args) { HelloServiceService helloServiceService   =new  HelloServiceService(); String word = helloServiceService.getHelloServicePort().sayHello("李帅"); System.out.println(word); System.out.println(helloServiceService.getHelloServicePort().getClass().getName()); }}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐