加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

教你简单玩转WebService -JAX-WS

发布时间:2020-12-16 22:34:41 所属栏目:安全 来源:网络整理
导读:WebService 是一种跨系统的调用的方式,他的特点有以下几点 1.跨系统 2.通过xml进行交流 3.通过http传输(其实底层是socket) 现在我给大家介绍jAX-WS 一种简单的webservice ? JAX-WS (JAVA API ?XML ?WEB SERVICE ) 步骤如下 1.建立一个java 工程(注意这个工

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()); }}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读