webservice接口的开发和调用
一、WebService的开发手段
使用Java开发WebService时可以使用以下两种开发手段
1、 使用JDK开发(1.6及以上版本)
2、使用CXF框架开发(工作中)
二、使用JDK开发WebService
2.1、开发WebService服务器端
1、定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所有方法,如下所示:
1 package me.gacl.ws;
2
3 import javax.jws.WebMethod;
4 import javax.jws.WebService;
5
6 /**
7 * @author gacl
8 * 定义SEI(WebService EndPoint Interface(终端))
9 */
10 //使用@WebService注解标注WebServiceI接口
11 @WebService
12 public interface WebServiceI {
13
14 //使用@WebMethod注解标注WebServiceI接口中的方法
15 @WebMethod
16 String sayHello(String name);
17
18 @WebMethod
19 String save(String name,String pwd);
20 }
2、编写interface的实现类,使用@WebService注解标注实现类,实现接口中定义的所有方法,如下所示:
1 package me.gacl.ws;
2
3 import javax.jws.WebService;
4
5 /**
6 * @author gacl
7 * SEI的具体实现
8 */
9 //使用@WebService注解标注WebServiceI接口的实现类WebServiceImpl
10 @WebService
11 public class WebServiceImpl implements WebServiceI {
12
13 @Override
14 public String sayHello(String name) {
15 System.out.println("WebService sayHello "+name);
16 return "sayHello "+name;
17 }
18
19 @Override
20 public String save(String name, String pwd) {
21 System.out.println("WebService save "+name+", "+pwd);
22 return "save Success";
23 }
24 }
3、使用Endpoint(终端)类发布webservice,代码如下:
1 package me.gacl.ws.test;
2
3 import javax.xml.ws.Endpoint;
4
5 import me.gacl.ws.WebServiceImpl;
6
7 /**
8 * @author gacl
9 *
10 * 发布Web Service
11 */
12 public class WebServicePublish {
13
14 public static void main(String[] args) {
15 //定义WebService的发布地址,这个地址就是提供给外界访问Webervice的URL地址,URL地址格式为:http://ip:端口号/xxxx
16 //String address = "http://192.168.1.100:8989/";这个WebService发布地址的写法是合法的
17 //String address = "http://192.168.1.100:8989/Webservice";这个WebService发布地址的是合法的
18 String address = "http://192.168.1.100:8989/WS_Server/Webservice";
19 //使用Endpoint类提供的publish方法发布WebService,发布时要保证使用的端口号没有被其他应用程序占用
20 Endpoint.publish(address , new WebServiceImpl());
21 System.out.println("发布webservice成功!");
22 }
23 }
运行WebServicePublish类,就可以将编写好的WebService发布好了,WebService的访问URL是:http://192.168.1.100:8989/WS_Server/Webservice ,如下图所示:
二、开发客户端
客户端调用我使用的两种方式
第一种使用apche cxf生成代码进行访问
1、下载apache cxf的包,地址为:http://cxf.apache.org/download.html 如:apache-cxf-3.1.6
2、解压apache-cxf-3.1.6到任意目录
3、配置环境变量
os系统设置
1)、export CXF_HOME=/Users/moon/Desktop/tools/apache-cxf-3.1.6
2)、path后面加 :$CXF_HOME/bin
windows系统设置
1)、CXF_HOME=D:\apache-cxf-3.1.6
2)、在path后面加上 %CXF_HOME%/bin;
在命令中输入wsdl2java,如果有提示usage,就表明配置成功
4、运行wsdl2java工具
在命令中输入:wsdl2java -d \xx\xxx\xx -client http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl
(\xx\xxx\xx 是客户端程序代码所在的目录,http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl 是发布的webservice服务)
附wsdl2java用法:
wsdl2java -p com -d D:\\src -all xx.wsdl
-p 指定其wsdl的命名空间,也就是要生成代码的包名:
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
生成后的代码直接放到client工程上面
另外新建一个client类 直接使用生成的类调用

package com.moon.cxf; import com.moon.cxf.client.Greeting;
import com.moon.cxf.client.GreetingImplService; public class CxfClient {
public static void main(String[] args) { GreetingImplService serviceFactory = new GreetingImplService();
Greeting service =
serviceFactory.getGreetingImplPort(); String result = service.greeting("Jaune");
System.out.println(result); }
}

二、使用axis调用webservice接口
引入axis 相关jar包
代码如下

package com.moon.cxf; import java.rmi.RemoteException; import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
/**
* 使用axis调用cxf发布的webservice接口
* @author moon
*
*/
public class AxisClient {
public static void main(String[] args) throws ServiceException, RemoteException {
try { String endpoint = " http://localhost:8080/cfxWSServer/webservice/Greeting";
// 调用过程
Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName("http://server.cxfWebservice.moon.com/","greeting"));// WSDL里面描述的操作名称 call.addParameter("username",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// 操作的参数 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型 call.setUseSOAPAction(true); // 给方法传递参数,并且调用方法
String temp = "good";
Object[] obj = new Object[] { temp };
String result = (String) call.invoke(obj); System.out.println("Result is : " + result);
} catch (Exception e) {
e.printStackTrace();
}
} }

相关代码:https://github.com/15210448683/WebServiceDemoImpl
webservice接口的开发和调用的更多相关文章
- Https Webservice接口的免证书调用
目录 前言 思路 方案 Axis调用 HttpClient调用 参考链接 前言 在调用https协议的Webservice接口时,如果没有做证书验证,一般会报javax.net.ssl.SSLHand ...
- Jaxb的优点与用法(bean转xml的插件,简化webservice接口的开发工作量)
一.jaxb是什么 JAXB是Java Architecture for XML Binding的缩写.可以将一个Java对象转变成为XML格式,反之亦然. 我们把对象与关系数据库之间的映射称 ...
- C#开发WEBService服务 C++开发客户端调用WEBService服务
编写WEBService服务端应用程序并部署 http://blog.csdn.net/u011835515/article/details/47615425 编写调用WEBService的C++客户 ...
- 『动善时』JMeter基础 — 50、使用JMeter测试WebService接口
目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...
- 『动善时』JMeter基础 — 51、使用JMeter测试WebService接口
目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...
- 基于Axis1.4的webservice接口开发(接口调用)
基于Axis1.4的webservice接口开发(接口调用) 一.webservice接口代码参考上一篇博客: http://www.cnblogs.com/zhukunqiang/p/7125668 ...
- 利用MyEclipse开发一个调用webservice接口的程序
上一篇文章我们已经学习了如何使用Java 工具MyEclipse开发一个webservice接口,那么接口开发好了如何调用?接下来我们就来解决这个问题. 1:首先随便创建一个Java project选 ...
- python开发笔记-python调用webservice接口
环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...
- Java调用webservice接口方法
java调用webservice接口 webservice的 发布一般都是使用WSDL(web service descriptive langu ...
随机推荐
- (95)Wangdao.com_第二十八天_进度事件
进度事件 进度事件 用来描述资源加载的进度, 主要由 AJAX 请求.<img>.<audio>.<video>.<style>.<link> ...
- react_app 项目开发 (2)_axios_pubsub-js
生产环境打包并运行 yarn run build 会src代码进行打包处理,在内存中生成打包文件 将打包文件保存至内存 yarn global add serve serve -s build 将 b ...
- linux学习:wget与lynx用法整理
指令:wget.lynx.axel wget url #下载数据写入文件,下载的文件名与url中的文件名保持一致,下载信息或进度写入stdoutwget url1 url2 url3 #下载多 ...
- 蓝桥杯-加法变乘法(java)
蓝桥杯第六届省赛题目-加法变乘法(java) 题目: 我们都知道:1+2+3+ ... + 49 = 1225 现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如: 1+2+3+... ...
- 太原面经分享:如何在vue面试环节,展示你晋级阿里P6+的技术功底?
前言 一年一度紧张刺激的高考开始了,与此同时,我也没闲着,奔走在各大公司的前端面试环节,不断积累着经验,一路升级打怪. 最近两年,太原作为一个准二线城市,各大互联网公司的技术栈也在升级换代,假如你在太 ...
- C++中的Public 、Private、Protected 区别
第一: private,public,protected的访问范围: private: 只能由该类的成员函数.友元的成员函数访问,不能被其他类的成员函数访问,即使是该类的对象也不能直接访问 publi ...
- DS博客作业01--线性表
1.本周学习总结(0--2分) 1.1思维导图 1.2.谈谈你对线性表的认识及学习体会. 1.线性表的内容上了三星期的课,相对来说内容比较丰富,尤其是链表方面,包含单链表,双链表和循环链表.作为第一部 ...
- shell 爬取图片下载到本地
#!/bin/bash #ddmm // #if [ -z $string ] 如果string 为空 #-z STRING the length of STRING is zero read -p ...
- 多线程之Lock的基本介绍
基本介绍 java.util.concurrent.locks是java1.5之后出现的一种锁实现方式,是一个接口.但是在这之前已经有一个同步机制的实现就是synchronized关键字,那为什么还要 ...
- 微信小程序wx.getLocation()获取经纬度及JavaScript SDK调用腾讯地图API获取某一类地址
简介 腾讯位置服务为微信小程序提供了基础的标点能力.线和圆的绘制接口等地图组件和位置展示.地图选点等地图API位置服务能力支持,使得开发者可以自由地实现自己的微信小程序产品. 在此基础上,腾讯位置服务 ...