利用JDK开发WebService简单实现
来源:互联网 发布:韩国文化产业知乎 编辑:程序博客网 时间:2024/05/16 23:50
开发环境:
eclipse+JDK8
首先开发服务器端:
1.定义SEI接口和其实现类
2.发布webservice
目录结构如下
①SEI接口:HelloWs.java
package com.service;/* *定义SEI接口,同时定义方法来接受客户端传参并提供返回值给客户端 */import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface HelloWs { @WebMethod public String getValue(String name);}
注意:@WebService和@WebMethod注解不可省略
②SEI接口实现类:HelloWsImpl.java
package com.service;import javax.jws.WebService;/* * SEI接口的实现类 */@WebServicepublic class HelloWsImpl implements HelloWs { @Override public String getValue(String name) { System.out.println("服务端调用getValue方法接收客户端传参:"+name); return "服务端返回值"; }}
③发布webservice:ServerTest.java
package com.test;import javax.xml.ws.Endpoint;import com.service.HelloWsImpl;public class ServerTest { public static void main(String[] args) { String address="http://localhost:8087/webservice/hello"; Endpoint.publish(address, new HelloWsImpl()); System.out.println("webservice发布成功!"); }}
运行ServerTest.java,控制台输出:
此时在浏览器地址栏输入:http://localhost:8888/webservice/hello?wsdl会显示:
此时服务端启动成功,webservice发布完成!
客户端开发:
首先建立客户端项目WSClient
然后在命令窗口中进入WSClient的src目录运行
wsimport -keep http://localhost:8888/webservice/hello?wsdl
没有错误的话就会出现上面图片中的内容。
回来刷新一下WSClient,会看到此时的项目目录结构如下:
现在不用改动自动生成的代码,新建一个客户端测试类ClientTest.java
来验证客户端与服务端能否正常交互
package com.test;import com.service.HelloWsImpl;import com.service.HelloWsImplService;public class ClientTest { public static void main(String[] args) { HelloWsImplService helloWsImplService=new HelloWsImplService(); HelloWsImpl helloWsImpl= helloWsImplService.getHelloWsImplPort(); String result=helloWsImpl.getValue("Huge"); System.out.println("客户端向服务端传参,收到服务端返回值:"+result); }}
启动客户端后:
服务端控制台输出:
客户端控制台输出:
大多数情况下,我们并不需要开发服务端,网上有很多可以使用的免费webservice接口。我们不用管服务端是用什么语言怎样开发的,只要知道如何客户端调用该服务端的资源就可以了,这也是webservice的优点之一!跨平台更跨语言!
比如http://www.webxml.com.cn/zh_cn/index.aspx,该网站就提供了很多免费webservice服务。
下面用一个简单的例子演示一下:
手机号码归属地查询
先找到URL(wsdl文件),然后dos命令窗口编译
最后编写客户端测试类
package cn.com.test;import cn.com.webxml.MobileCodeWS;import cn.com.webxml.MobileCodeWSSoap;public class ClientTest { public static void main(String[] args) { MobileCodeWS mobileCodeWS=new MobileCodeWS(); MobileCodeWSSoap mobileCodeWSSoap=mobileCodeWS.getMobileCodeWSSoap(); String result=mobileCodeWSSoap.getMobileCodeInfo("15677278945", null); System.out.println(result); }}
输出:
- 利用JDK开发WebService简单实现
- JDK实现WebService简单实例
- 使用JDK开发一个简单的WebService
- 利用jdk使用WebService
- 利用jdk开发web service简单实例
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用jdk开发webservice
- jdk开发webservice例子
- 使用JDK开发WebService
- webservice之jdk实现
- 【webservice开发】之使用JDK开发WebService
- WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单
- structs利用 FileUtils 类文件上传示例
- JQuery 加载并解析 XML实现省市联动
- linux下创建软连接和硬链接
- <转>hadoop学习之hadoop集群功能简单测试验证
- python字符转码问题-持续更新
- 利用JDK开发WebService简单实现
- iOS NSScanner类的基本用法详解
- LeetCode84. Largest Rectangle in Histogram
- Spark 数据ETL
- 设置焦点注意
- おひきは本当にいじめ彼女と呼ばれる
- Linux下Mysql的odbc配置
- mybatis oracle使用like 包含%
- redis在Java web项目的简单应用