一、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. (90)Wangdao.com第二十三天_JavaScript CSS 操作

    CSS 操作 通过 JavaScript 操作 CSS HTML 元素的 style 属性 <div style="background-color:red; border:1px s ...

  2. [LeetCode] Find Eventual Safe States 找到最终的安全状态

    In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  ...

  3. python递归

    一.递归 (1)递归就是函数自己调用自己的过程: (2)使用递归时,需要注意递归的出口,明确递归的终止条件. #计算n的阶乘 def fun(n): if n==1: return 1 else: r ...

  4. HTTP学习总结

    首先是一张总结的图: 对各个不同的通信进行解分: 1.http通信详解 2.cookie通信图: 3.cookie管理的session信息 4.token通信

  5. 由PHP实现单向链表引发的对象赋值,对象传参,链表操作引发的一系列问题

    2019年2月25日14:21:13 测试版本php 5.4 ,5.6,7.0,7.2 代码请看: https://www.cnblogs.com/zx-admin/p/10373866.html 1 ...

  6. 一篇文章学懂Shell脚本,最简明的教程在这里

    Shell脚本,就是利用Shell的命令解释的功能,对一个纯文本的文件进行解析,然后执行这些功能,也可以说Shell脚本就是一系列命令的集合. Shell可以直接使用在win/Unix/Linux上面 ...

  7. JVM内存模型与垃圾回收

    内存模型 1,程序计数器(Program Counter Register):程序计数器是一个比较小的内存区域,用于指示当前线程所执行的字节码执行到了第几行,可以理解为是当前线程的行号指示器.字节码解 ...

  8. nginx匹配规则说明以及匹配的优先级

    location 匹配规则语法规则 location [=|~|~*|^~] /uri/ { … } 模式    含义location = /uri    = 表示精确匹配,只有完全匹配上才能生效lo ...

  9. 【mybatis】mybatis中 <if test=>等于的条件怎么写

  10. How to Preloading content with rel preload

    The preload value of the <link> element's rel attribute allows you to write declarative fetch ...