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

WebService学习记录

发布时间:2020-12-16 22:35:03 所属栏目:安全 来源:网络整理
导读:? ? 最近项目要使用WebService,正在学习中,做个记录。 ? ? WebService是一个 平台 独立的,低耦合的,自包含的、基于可 编程 的web的应用程序,可使用开放的 XML ( 标准通用标记语言 下的一个子集) 标准 来 描述 、发布、发现、协调和配置这些应用程序,

? ? 最近项目要使用WebService,正在学习中,做个记录。

? ? WebService是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML标准通用标记语言下的一个子集)标准描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序

? ? 巴拉巴拉。。。。。。

? ? 本农使用的JDK自带的WebService编写。

? ? 1、开始编写接口

package?team.soi.service;

/**
?*?Created?by?soi?on?15-10-20.
?*/
public?interface?HelloService?{

????/**
?????*?to?do?sth.
?????*?@param?to
?????*?@return
?????*/
????Object?toDoSth(String?to);
}

? ? 2、编写接口的实现类

package?team.soi.service.impl;

import?team.soi.service.HelloService;

import?javax.jws.WebService;

/**
?*?Created?by?soi?on?15-10-20.
?*/
@WebService
public?class?HelloServiceImpl?implements?HelloService?{

????public?Object?toDoSth(String?to)?{
????????return?"Hello,"?+?to?+?"!?Welcome?to?my?webservice?world!";
????}
}

? ? 3、发布WebService

? ??

package?team.soi;

import?team.soi.service.impl.HelloServiceImpl;

import?javax.xml.ws.Endpoint;

/**
?*?Hello?world!
?*/
public?class?App?{
????public?static?void?main(String[]?args)?{
????????Endpoint.publish("http://localhost:8899/ws/demo",?new?HelloServiceImpl());
????}
}

? 奏是介么仍性,在浏览器输入

http://localhost:8899/ws/demo?wsdl

? 见证奇迹的时刻到了,Duang......如下:

This?XML?file?does?not?appear?to?have?any?style?information?associated?with?it.?The?document?tree?is?shown?below.
<!--
?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.service.soi.team/"?xmlns:xsd="http://www.w3.org/2001/XMLSchema"?xmlns="http://schemas.xmlsoap.org/wsdl/"?targetNamespace="http://impl.service.soi.team/"name="HelloServiceImplService">
<types>
<xsd:schema>
<xsd:import?namespace="http://impl.service.soi.team/"?schemaLocation="http://localhost:8899/ws/demo?xsd=1"/>
</xsd:schema>
</types>
<message?name="toDoSth">
<part?name="parameters"?element="tns:toDoSth"/>
</message>
<message?name="toDoSthResponse">
<part?name="parameters"?element="tns:toDoSthResponse"/>
</message>
<portType?name="HelloServiceImpl">
<operation?name="toDoSth">
<input?wsam:Action="http://impl.service.soi.team/HelloServiceImpl/toDoSthRequest"?message="tns:toDoSth"/>
<output?wsam:Action="http://impl.service.soi.team/HelloServiceImpl/toDoSthResponse"?message="tns:toDoSthResponse"/>
</operation>
</portType>
<binding?name="HelloServiceImplPortBinding"?type="tns:HelloServiceImpl">
<soap:binding?transport="http://schemas.xmlsoap.org/soap/http"?style="document"/>
<operation?name="toDoSth">
<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://localhost:8899/ws/demo"/>
</port>
</service>
</definitions>

? ?到这里,说明你的WebService服务端编写完成了,接下来,我们要怎么去调用呢?

? ?说真的,在今天之前,我就知道WebService可以如上的写法,但是不会调用,感觉自己好Low,于是乎,在群里面一吼,很多热心的人回答了我的问题,但是,无法满足我的需求。最后问了一个工作上有对接的朋友,得知了接下来该做的事情。

? ?使用JDK的命令:

? ?

soi@soi:~/workspace/wsc$?wsimport?-extension?-keep?-p?team.soi.ws.client?-s?./src?-d?./bin?http://localhost:8899/ws/demo?wsdl
正在解析?WSDL...

正在生成代码...

正在编译代码...

soi@soi:~/workspace/wsc$?cd?bin
soi@soi:~/workspace/wsc/bin$?jar?cvf?hello-ws-demo.jar?team
soi@soi:~/workspace/wsc/bin$?ls
hello-ws-demo.jar??team

? 此时,我们已经拿到了本WebService的客户端jar包,我们将客户端jar包加入到我们的工程,顺理成章,开始编写客户端代码:

package?team.soi;

import?junit.framework.TestCase;
import?team.soi.ws.client.HelloServiceImpl;
import?team.soi.ws.client.HelloServiceImplService;

/**
?*?Unit?test?for?simple?App.
?*/
public?class?AppTest
????????extends?TestCase?{

????/**
?????*?test?ws
?????*/
????public?void?testWs()?{
????????HelloServiceImpl?service?=?new?HelloServiceImplService().getHelloServiceImplPort();
????????String?s?=?(String)?service.toDoSth("Soi");
????????System.out.println(s);
????}
}

? 运行测试代码:

?

Hello,Soi!?Welcome?to?my?webservice?world!

? 撸完收工.......

(编辑:李大同)

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

    推荐文章
      热点阅读