一、工具

1、下载 Axis2以及eclipse的Axis2插件。http://axis.apache.org/axis2/java/core/download.cgi

2、axis2-1.7.1-war.zip解压,将压缩包内的axis2.war部署到%TOMCAT-HOME%/webapps下,启动tomcat,访问http://localhost:8080/axis2/看是否正常。

点击Service会进入Service列表页面,当前只有一个Version服务。http://localhost:8080/axis2/services/Version?wsdl

3、解压缩eclipse插件 axis2-eclipse-codegen-plugin-1.7.1.zip,axis2-eclipse-service-plugin-1.7.1.zip。解压后将plugins 复制到%ECLIPSE_HOME%\plugins。

二、Axis2发布Webservice

a、新建名称为Axis2Service1 的java工程。新建TestWs.java

b.1、打包部署--arr部署方式

有手动打包和插件打包两种方式,在此我用了插件打包的方式

  1. 手动打包

    • 新建\Axis2Service1\deploy文件夹,将\Axis2Service1\bin下的class文件复制过来。
    • 新建\Axis2Service1\deploy\META-INF\services.xml文件
<service name="AxisService"><description>AxisService</description><parameter name="ServiceClass">ws.TestWs</parameter><operation name="showName"><messageReceiverclass="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /></operation><operation name="getName"><messageReceiverclass="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /></operation>
</service>

生成aar包 \Axis2Service1\deploy>jar cvf AxisService.aar . (注意带.号)

2 . 插件打包

  • IDE中选择New->other->Axis2 Service Archiver,点击Next;
  • Class File Location:选择Axis2Service1\bin目录,点击Next;
  • 勾选Skip WSDL,点击Next;
  • Service Archiver 选择jar位置,如果没有jar包就直接点击Next;
  • 勾选Generate the service xml automatically 自动生成service.xml file文件,点击Next
  • service name,输入:AxisService,然后在class name 中填写要发布的类(全路径,如:ws.TestWs),点击load。勾选 Search declared methods only。点击next
  • output File location,输入:D:\ ; output File Name,输入artiver文件的名称 AxisService。点击finish。
  • 提示 Service Archvie generated successfully! 注册表明,生成成功。

3、发布AxisService

AxisService.aar复制到%TOMCAT-HOME%/webapps/axis2/WEB-INF/services下。(不打aar包,\Axis2Service1\deploy下面复制过去也是可以)

打开http://localhost:8080/axis2/services/listServices 就可以看到刚才发布的AxisService服务了,下面有两个函数:showName,getName。

b.2 独立部署

1、新建java web project工程。

2、文件复制

%TOMCAT-HOME%\webapps\axis2\WEB-INF\lib 复制到 \Axis2Service2\WebRoot\WEB-INF\lib 下,并加入工程引用。

%TOMCAT-HOME%\webapps\axis2\WEB-INF\conf 复制到 \Axis2Service2\WebRoot\WEB-INF\conf

%TOMCAT-HOME%\webapps\axis2\WEB-INF\modules 复制到 \Axis2Service2\WebRoot\WEB-INF\modules

3、web.xml 代码如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="wmf" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet><servlet-name>AxisServlet</servlet-name><servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>AxisServlet</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping>
</web-app>

2、新建 \Axis2Service2\src\ws\TestWs.java

package ws;
public class TestWs {public String showName(String name) {return name; }public String getName() {return "Axis2Service Sample"; }
}

3、新建\Axis2Service2\WebRoot\WEB-INF\services目录。

4、新建一个AxisService服务

AxisService\META-INF\services.xml

<service name="AxisService"><description>AxisService</description><parameter name="ServiceClass">ws.TestWs</parameter><operation name="showName"><messageReceiverclass="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /></operation><operation name="getName"><messageReceiverclass="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /></operation>
</service>

启动tomcat后,访问http://localhost:8085/Axis2Service2/services/AxisService?wsdl看是否正常。

三、AXIS2调用Web Services

一、客户端stub文件生成

我用的是插件生成方式

1、脚本生成方式

去AXIS2的解压目录下bin(%AXIS2_HOME%\bin\)下执行下面语句

wsdl2java -uri http://localhost:8085/Axis2Service2/services/AxisService?wsdl -p ws -s -o stub

-p参数指定了生成的Java类的包名

-o参数指定了生成的一系列文件保存的根目录

在stub\src\ws自动生成AxisServiceStub.java

2、插件生成方式

IDE中选择New->other->Axis2 Code Generator,点击Next;

勾选Generate Java source code from a WSDL file,点击Next;

WSDL file location,输入:http://localhost:8085/Axis2Service2/services/AxisService?wsdl,点击Next;

如果路径不对会提示:Specified WSDL is invalid!, Please select a validated *.wsdl/*.xml file on previous page. 正确的话,点击next;

指定输入路径,点击Next 提示:All operations completed successfully! 生成成功。

在D:\src\ws 自动生成了stub一系列文件,其中ws是包名。

上面2种方式生成的stub类有点不一样,脚本生成方式是单一文件,插件生成方式生成的一系列文件。

二、客户端调用 脚本生成方式为例子,插件生成的类似。

1、新建 java工程 Axis2Client

新建\Axis2Client\lib文件夹 将%AXIS2_HOME%\lib\ 下的所有jar包复制到\Axis2Client\lib,并加入工程引用中,加入工程引用的方式如下

点击工程右键--》Build Path --->Configure Build Paht--->

将通过插件生成在stub文件加入到src\ws下,(若是脚本生成方式,则是单一AxisServiceStub.java文件加入到src\ws下 )

2、新建TestWs.java,在test包下 主要代码如下

//初始化Sub类
AxisServiceStub stub = new AxisServiceStub();
//传递AxisServiceStub.ShowName对象,相关参数在这边赋值。
AxisServiceStub.ShowName command = new AxisServiceStub.ShowName();
command.setName("Hello!");
//取得返回值
String name = stub.showName(command).get_return();
System.out.println(name);

调用成功后控制台输出:Hello!

扩展学习---AXIS2调用REST Web Services

使用http://localhost:8086/Axis2Rest/services/AxisService/showName?name=rest的方式访问刚才发布成功的WebService。从上面可以看出这个就是rest风格。

Axis1.0是无法通过showName?name=rest来获取信息的。

2、使用axis客户端调用

public class TestRest {private static String toEpr = "http://localhost:8086/Axis2Rest/services/AxisService";public static void main(String[] args) throws AxisFault {Options options = new Options();options.setTo(new EndpointReference(toEpr));//客户端REST方式调用服务跟普通服务的区别,REST调用必须加上下面这个代码。
        options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);ServiceClient sender = new ServiceClient();//axis2-1.5.4不需要下面这句代码,否则会报错//sender.engageModule(new QName(Constants.MODULE_ADDRESSING));
        sender.setOptions(options);OMElement result = sender.sendReceive(getPayload());try {XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);result.serialize(writer);writer.flush();} catch (XMLStreamException e) {e.printStackTrace();} catch (FactoryConfigurationError e) {e.printStackTrace();}}private static OMElement getPayload() {OMFactory fac = OMAbstractFactory.getOMFactory();OMNamespace omNs = fac.createOMNamespace("http://ws", "example1");OMElement method = fac.createOMElement("showName", omNs);OMElement value = fac.createOMElement("name", omNs);value.addChild(fac.createOMText(value, "Rest"));method.addChild(value);return method;}

说明:

1、sender.engageModule(new QName(Constants.MODULE_ADDRESSING)); axis2-1.5.4不需要下面这句代码,否则会报错

2、客户端REST方式调用服务跟普通服务的区别,就是Rest有下面这个代码;

options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);

两者返回的数据都是

<ns:showNameResponse xmlns:ns="Resthttp://ws"><ns:return>Rest</ns:return></ns:showNameResponse>

3、getPayload方法

OMNamespace omNs = fac.createOMNamespace("http://ws", "example1");

指定命名空间,如果没对的话会报如下错误namespace mismatch require http://ws found http://ws1 OMElement method = fac.createOMElement("showName", omNs);

要传递的方法名为 "showName" OMElement value = fac.createOMElement("name", omNs);

传递的参数为name value.addChild(fac.createOMText(value, "Rest"));

传递参数name的值为Rest。

转载于:https://www.cnblogs.com/qingblog/p/5327848.html

webService发布和调用--Axis2相关推荐

  1. C# WebService发布与调用方法(转)

    Web Service是可操作的分布式应用程序.它以SOAP作为基本通信协议,使用HTTP和XML进行可以穿越防火墙的自由通信. 错误提示: * 出现 访问 IIS 元数据库失败 * iis 无法显示 ...

  2. Apache CXF实现WebService发布和调用

    第一种方法:不用导入cxf jars 服务端: 1. 新建Web工程 2.新建接口和实现类.测试类 目录结构图如下: 接口代码: package com.cxf.spring.service;impo ...

  3. Axis2 webservice入门--Webservice的发布与调用

    一.Webservice发布 参考 http://www.cnblogs.com/demingblog/p/3263576.html 二.webservice 调用 部分参考:http://www.c ...

  4. PHP Webservice的发布与调用

    PHP Webservice的发布与调用 1.  环境配置 配置php.ini,把php_soap.dll前面的分号去掉, 不然会报错 class soapserver not found 重启apa ...

  5. 【WebService笔记01】使用JWS实现WebService接口的发布和调用

    这篇文章,主要介绍如何使用JWS实现WebService接口的发布和调用. 目录 一.JWS实现WebService接口 1.1.JWS发布WebService接口 (1)编写接口 (2)编写实现类 ...

  6. SpringBoot集成Axis2,部署webservice接口并调用

    SpringBoot集成Axis2,部署webservice接口并调用 一.省略新建springboot项目过程 1.pom.xml 2.application.yml 3.准备工作做完,编写serv ...

  7. springboot集成webservice发布

    远程调用本人通常使用restful,webservice不太会用,今天先学习记录下,所以有不对的对方请指正! 参考:https://blog.csdn.net/h_j_c_123/article/de ...

  8. 金蝶 EAS WebService 发布过程

    金蝶 EAS WebService 发布过程 1. 打开BOS开发工具–>右上角切换:金蝶BOS设计开发工具 (1)左边目录右键新建 功能,点开 功能 方法列表点击 增加 方法: (2)右键 功 ...

  9. 用cxf发布和调用web service

    用cxf发布和调用web service http://cxf.apache.org/docs/jax-ws-configuration.html  官方API helloword地址 用CXF来做w ...

最新文章

  1. CG-CTF-Web-MYSQL
  2. mysql 搭建日志服务器_一、架构01-搭建日志服务器Rsyslog
  3. 45 MM配置-采购-条件-定价过程-定义定价计算方案
  4. 蓝牙配对不弹配对框_配对编程在工作场所的好处和陷阱
  5. 数据意识崛起,从企业应用看BI软件的未来发展 1
  6. Linux常用50条命令
  7. PSP3000/2000V3用5.03GEN-C安装教程
  8. 又一家流血上市!AI四小龙之首商汤科技冲刺IPO,三年半巨亏242亿
  9. 如何检测文档里含有非英字符_知网检测是算字数还是字符
  10. 昆明计算机设计学院官网,文山高中考不上有什么出路
  11. 中通快递api,中通快递一件代发api,中通快递礼品商城api,中通快递空包api
  12. 唯样商城:芯片解密的优缺点有哪些
  13. 如何画五子棋棋盘c语言easyx,大佬们,用c++怎么画五子棋棋盘呢?
  14. 椭圆机和跑步机哪个更好
  15. Java实现 蓝桥杯 算法训练 未名湖边的烦恼
  16. 从多个数中取出之和等于定值的组合
  17. DSP28335的硬件SPI使用(无FIFO)总结
  18. 论文阅读——用于脑机接口的运动意图和运动想象脑电数据分析研究
  19. 亚洲最大高考工厂打造书呆子流水线?
  20. Overview of technical writing courses | 技术写作课程概览

热门文章

  1. redis各数据结构的使用场景
  2. bash脚本之条件判断
  3. Istio 1.0 部署
  4. WhatsApp被曝内部收集用户数据
  5. Canonical 创始人:考虑让 Ubuntu 支持鹦鹉桌面
  6. 菜鸟学Linux 第030篇笔记 yum使用,源码编译安装
  7. C#根据屏幕尺寸大小重新定位控件大小和位置
  8. 常用牛人主页链接(计算机视觉、模式识别、机器学习相关方向,陆续更新。。。。)...
  9. Catalyst 6000 family Architecture
  10. Jquery之AJAX用户注册表单验证(Vancl版)