WebService学习记录
2015-10-20 00:00
344 查看
摘要: 最近项目要使用WebService,正在学习中,做个记录。
最近项目要使用WebService,正在学习中,做个记录。
WebService是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。
巴拉巴拉。。。。。。
本农使用的JDK自带的WebService编写。
1、开始编写接口
2、编写接口的实现类
3、发布WebService
奏是介么仍性,在浏览器输入
见证奇迹的时刻到了,Duang......如下:
到这里,说明你的WebService服务端编写完成了,接下来,我们要怎么去调用呢?
说真的,在今天之前,我就知道WebService可以如上的写法,但是不会调用,感觉自己好Low,于是乎,在群里面一吼,很多热心的人回答了我的问题,但是,无法满足我的需求。最后问了一个工作上有对接的朋友,得知了接下来该做的事情。
使用JDK的命令:
此时,我们已经拿到了本WebService的客户端jar包,我们将客户端jar包加入到我们的工程,顺理成章,开始编写客户端代码:
运行测试代码:
撸完收工.......
最近项目要使用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!
撸完收工.......
相关文章推荐
- Lisp-1和Lisp-2
- 前言 -- 数据库准备、一些名词准备
- 第一章 实体框架入门
- getSharedPreferences只能继承Activity才能使用
- 理解Fragment生命周期
- git常用技能(转)
- 企业级svnserve配置方案
- Maven3仓库与配置
- Array转String
- 金字塔原理-基本概念
- mysql sql优化
- js立即执行函数的使用
- buntu 12.04 server网络配置
- html5 canvas 与小丑。
- 实时监听js改变value,跨浏览器支持
- There was an internal API error.的解决办法
- 设置Hadoop+Hbase集群pid文件存储位置
- memcache在linux上配置服务日志
- 关于Jersey整合Spring注入出现NullPointException问题
- 清除行内元素之间HTML空白的几种解决方案