一、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接口的开发和调用的更多相关文章

  1. Https Webservice接口的免证书调用

    目录 前言 思路 方案 Axis调用 HttpClient调用 参考链接 前言 在调用https协议的Webservice接口时,如果没有做证书验证,一般会报javax.net.ssl.SSLHand ...

  2. Jaxb的优点与用法(bean转xml的插件,简化webservice接口的开发工作量)

    一.jaxb是什么 JAXB是Java Architecture for XML Binding的缩写.可以将一个Java对象转变成为XML格式,反之亦然.     我们把对象与关系数据库之间的映射称 ...

  3. C#开发WEBService服务 C++开发客户端调用WEBService服务

    编写WEBService服务端应用程序并部署 http://blog.csdn.net/u011835515/article/details/47615425 编写调用WEBService的C++客户 ...

  4. 『动善时』JMeter基础 — 50、使用JMeter测试WebService接口

    目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...

  5. 『动善时』JMeter基础 — 51、使用JMeter测试WebService接口

    目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...

  6. 基于Axis1.4的webservice接口开发(接口调用)

    基于Axis1.4的webservice接口开发(接口调用) 一.webservice接口代码参考上一篇博客: http://www.cnblogs.com/zhukunqiang/p/7125668 ...

  7. 利用MyEclipse开发一个调用webservice接口的程序

    上一篇文章我们已经学习了如何使用Java 工具MyEclipse开发一个webservice接口,那么接口开发好了如何调用?接下来我们就来解决这个问题. 1:首先随便创建一个Java project选 ...

  8. python开发笔记-python调用webservice接口

    环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...

  9. Java调用webservice接口方法

                             java调用webservice接口   webservice的 发布一般都是使用WSDL(web service descriptive langu ...

随机推荐

  1. 转载:[Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)

    原文:http://www.cnblogs.com/wang-meng/p/5898837.html 一:继承.抽象类与接口区别.访问控制(private, public, protected,默认) ...

  2. Web版记账本开发记录(二)开发过程遇到的问题小结1 对数据库的区间查询

    问题1 对数据库的区间查询 如功能显示,想要按照年份和月份查询相应的记录,就要使用区间查询 对应的代码如下 servlet层的ChaXun java.sql.Date sDate = new java ...

  3. js高级4

    1.Date  类 getDate()返回一个月中的某一天 1-31 getDay() 返回一周中的某一天0-6 getFullyear()返回四位数的年份 getMonth()返回月份 比实际情况小 ...

  4. C++接口与实现的抽象分离

    IPerson.h #ifndef I_PERSON_H_ #define I_PERSON_H_ #include <string> #include <ostream> c ...

  5. 关于Spring事物的面试题

    https://blog.csdn.net/h294590501/article/details/80386000 数据库事务和Spring事务是一般面试都会被提到,很多朋友写惯了代码,很少花时间去整 ...

  6. Android创建列表并为列表添加数据

    在Android开发中经常会用到ListView,也就是列表,而列表的内容需要如何决定呢,很好的方法是使用各种adapter 首先来看看用entries添加列表内容 在布局文件中,创建一个ListVi ...

  7. COMP9021 PRINCIPLES OF PROGRAMMING

    QUIZ 7COMP9021 PRINCIPLES OF PROGRAMMING$ python3 quiz_7.pyEnter four integers: 0 2 2 8Here is the g ...

  8. 2018-2019-2 网络对抗技术 20165317 Exp5 MSF基础应用

    2018-2019-2 网络对抗技术 20165317 Exp5 MSF基础应用 一.原理与实践说明 1.实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具 ...

  9. 学习lambda表达式总结

    因为最近开发涉及到大量的集合数据处理,就开始研究lambda表达式使用,看了<Java8函数式编程>,同时研究了不少博客,总结了一些基础的用法,写一篇博客,为以后的使用提供便利. 下面介绍 ...

  10. Python环境——安装扩展库

    一.修改easy_install源 在操作用户家目录添加一个文件 cat >> ~/.pydistutils.cfg <<EOF [easy_install] index-ur ...