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 ...
随机推荐
- Go语言基础之流程控制
Go语言基础之流程控制 流程控制是每种编程语言控制逻辑走向和执行次序的重要部分,流程控制可以说是一门语言的“经脉”. Go语言中最常用额流程控制有if和for,而switch和goto主要是为了简化代 ...
- 《Linux内核原理与分析》第一周作业 20189210
实验一 Linux系统简介 这一节主要学习了Linux的历史,Linux有关的重要人物以及学习Linux的方法,Linux和Windows的区别.其中学到了LInux中的应用程序大都为开源自由的软件, ...
- Prometheus 和 Grafana 安装部署
Prometheus 是一套开源的系统监控报警框架.Prometheus 作为生态圈 Cloud Native Computing Foundation(简称:CNCF)中的重要一员,其活跃度仅次于 ...
- Java 中 String 的字面量与 intern 方法
下方代码主要说明: String b = new String("xyz") 创建2个对象,一个在常量池中的 "xyz",一个 String 实例对象,返回的 ...
- DevExpress GridControl复合表头(多行表头)设置
关于DevExpress.XtraGrid的复合表头或多行表头的示例,界面如下图所示 1.首先要把DevExpress的GridControl转换为BandedGridView 2.设置显示列及绑定的 ...
- 使用mysql设计一个全局订单生产计数器
2018年8月10日08:53:50 一般生产订单号的方式 1,使用时期+随机数1+随机数2 缺点,有可能在并发的时候会出现重复,解决办法就是加唯一索引,在插入数据的做查询是否已经被使用 2,使用时间 ...
- Python全栈-magedu-2018-笔记1
第一章 - Python 环境搭建 操作系统准备 准备Linux最小系统即可. 如果在虚拟机中克隆,MAC地址会变. 这里使用CentOS 6.5+ Pyenv 安装 安装git yum instal ...
- 是否能设计一种DNN的特定网络结构来改善DNN,使得其学习起来更加高效
小结: 1. 是否能设计一种DNN的特定网络结构来改善DNN,使得其学习起来更加高效 https://mp.weixin.qq.com/s/lF_WLAn6JyQqf10076hsjA Deep &a ...
- Express全系列教程之(六):cookie的使用
一.关于Cookie 在我们关闭一个登录过的网址并重新打开它后,我们的登录信息依然没有丢失:当我们浏览了商品后历史记录里出现了我们点击过的商品:当我们推回到首页后,推荐商品也为我们选出了相似物品:事实 ...
- oracle学习笔记第二天
一.连接查询 --笛卡尔积(表 * 表),连接的基础select * from emp,dept;--等值连接select * from emp e,dept d where e.deptno = d ...