`
mamacmm
  • 浏览: 197360 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

Axis2简单入门

    博客分类:
  • J2EE
阅读更多

1.到Apache官网下载Axis2

 

给你地址:http://axis.apache.org/axis2/java/core/download.cgi

 

2.设置环境

 

把下载好的Axis2放到一个你喜欢的位置,我放在了

F:\Program Files\axis2-1.5.5

 

然后设置环境变量:

AXIS2_HOME=F:\Program Files\axis2-1.5.5
在PATH 最后添加 ;%AXIS2_HOME%\bin

 

3.简单测试

 

%AXIS2_HOME%\bin\axis2server.bat (Windows)

执行这个脚本,会看到:

Using JAVA_HOME    C:\Program Files\Java\jdk1.7.0
Using AXIS2_HOME   F:\Program Files\axis2-1.5.5
[INFO] [SimpleAxisServer] Starting
[INFO] [SimpleAxisServer] Using the Axis2 RepositoryF:\Program Files\axis2-1.5.5\repository
[SimpleAxisServer] Using the Axis2 RepositoryF:\Program Files\axis2-1.5.5\repository
[SimpleAxisServer] Using the Axis2 Configuration FileF:\Program Files\axis2-1.5.5\conf\axis2.xml
[INFO] Clustering has been disabled
[INFO] Deploying module: addressing-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/ad
dressing-1.5.5.mar
[INFO] Deploying module: metadataExchange-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modu
les/mex-1.5.5.mar
[INFO] Deploying module: mtompolicy-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/mt
ompolicy-1.5.5.mar
[INFO] Deploying module: ping-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/ping-1.5
.5.mar
[INFO] Deploying module: script-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/script
ing-1.5.5.mar
[INFO] Deploying module: soapmonitor-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/s
oapmonitor-1.5.5.mar
[INFO] Deploying Web service: version.aar - file:/F:/Program Files/axis2-1.5.5/repository/services/v
ersion.aar
[INFO] [SimpleAxisServer] Started
[SimpleAxisServer] Started
[INFO] Listening on port 8080

 

 

在浏览器里面输入:http://localhost:8080/axis2/services/

 

如果看到:

 

Deployed servicesVersion
Available operations

  • getVersion

 

这说明Axis2没问题

 

4.编译Axis2项目

 

首先,你应该下载了 Ant ,并且版本要大于 1.6.5 ,设置好Ant环境变量 ,然后再进行下面的操作

 

下面进入到 $AXIS_HOME/webapp 下

F:\Program Files\axis2-1.5.5\webapp>ant -version
Apache Ant(TM) version 1.8.2 compiled on December 20 2010
F:\Program Files\axis2-1.5.5\webapp>ant create.war
Buildfile: F:\Program Files\axis2-1.5.5\webapp\build.xml
init:
    [mkdir] Created dir: F:\Program Files\axis2-1.5.5\dist
    [mkdir] Created dir: F:\Program Files\axis2-1.5.5\dist\temp
     [copy] Copying 59 files to F:\Program Files\axis2-1.5.5\dist\temp
prepare.repo:
     [copy] Copying 9 files to F:\Program Files\axis2-1.5.5\dist\temp\WEB-INF
    [mkdir] Created dir: F:\Program Files\axis2-1.5.5\dist\temp\WEB-INF\conf
     [copy] Copying 1 file to F:\Program Files\axis2-1.5.5\dist\temp\WEB-INF\conf
create.war:
      [war] Building war: F:\Program Files\axis2-1.5.5\dist\axis2.war
   [delete] Deleting directory F:\Program Files\axis2-1.5.5\dist\temp
BUILD SUCCESSFUL
Total time: 4 seconds
F:\Program Files\axis2-1.5.5\webapp>

 
这样,便编译好了Axis2的项目。

 

5.测试Axis2项目

 

准备使用 Tomcat容器,所以,没有的先下载。

 

刚刚build好的Axis2项目应该在 AXIS2_HOME/dist 下面

 

把这个 axis2.war  剪切(或者复制)到tomcat下的webapp目录下,启动Tomcat(启动前把刚刚打开的Axis服务关掉,因为都用了8080端口)。

 

 

再输入下面的地址:

 

 http://localhost:8080/axis2/axis2-web/index.jsp

 

 如果看到这样的结果:

 

Welcome!

 

Welcome to the new generation of Axis. If you can see this page you have successfully deployed the Axis2 Web Application. However, to ensure that Axis2 is properly working, we encourage you to click on the validate link.

  • Services
    View the list of all the available services deployed in this server.
  • Validate
    Check the system to see whether all the required libraries are in place and view the system information.
  • Administration
    Console for administering this Axis2 installation.
<!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more contributor license agreements. See the NOTICE file ~ distributed with this work for additional information ~ regarding copyright ownership. The ASF licenses this file ~ to you under the Apache License, Version 2.0 (the ~ "License"); you may not use this file except in compliance ~ with the License. You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, ~ software distributed under the License is distributed on an ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~ KIND, either express or implied. See the License for the ~ specific language governing permissions and limitations ~ under the License. -->

 

说明成功了。

 

点击 Services 可查看当前存在的服务。

 

 

6.编写测试服务

 

 使用Eclipse新建一个Java项目

 

导入所需jar包:当然就是 %AXIS2_HOME%\lib  下面的所有jar包(为了方便)。

 

新建包:

 

mm.test.webservices.axis2userguide

 

在这个包下面

创建 SampleService.java

 

内容如下:

 

package mm.test.webservices.axis2userguide;

import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

public class SampleService {

	public OMElement sayHello(OMElement element) throws XMLStreamException {
		element.build();
		element.detach();

		String rootName = element.getLocalName();
		System.out.println("Reading " + rootName + " element");

		OMElement childElement = element.getFirstElement();
		String personToGreet = childElement.getText();

		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace(
				"http://example1.org/example1", "example1");
		OMElement method = fac.createOMElement("sayHelloResponse", omNs);
		OMElement value = fac.createOMElement("greeting", omNs);
		value.addChild(fac.createOMText(value, "Hello, " + personToGreet));
		method.addChild(value);

		return method;
	}

	@SuppressWarnings("unused")
	private void ping() {
	}

}

 

然后再到另一个地方(随意,当然,当前项目下也可以)创建一个文件夹:

 

 J:\SampleService

 

 这是我的。

 

然后,把刚才编译好的 mm.test.webservices.axis2userguide.SampleService.class

连带文件夹拷到这个目录下

 

再在当前目录下新建:

 

META-INF

 

在META-INF目录下新建文件:services.xml,内容如下:

 

<service name="UserGuideSampleService">
    <description>
        This is a sample service created in the Axis2 User's Guide
    </description>

    <parameter name="ServiceClass">
    	mm.test.webservices.axis2userguide.SampleService
		</parameter>

    <operation name="sayHello">
        <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
    </operation>
    <operation name="ping">
        <messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
    </operation>
</service>

 

还是在J:\SampleService文件夹下,打开cmd窗口,输入:

J:\SampleService>jar cvf SampleService.aar ./*
标明清单(manifest)
忽略项 META-INF/
增加:META-INF/services.xml(读入= 549) (写出= 243)(压缩了 55%)
增加:mm/(读入= 0) (写出= 0)(存储了 0%)
增加:mm/test/(读入= 0) (写出= 0)(存储了 0%)
增加:mm/test/webservices/(读入= 0) (写出= 0)(存储了 0%)
增加:mm/test/webservices/axis2userguide/(读入= 0) (写出= 0)(存储了 0%)
增加:mm/test/webservices/axis2userguide/SampleService.class(读入= 2232) (写出= 1074)(压缩了 51%)
J:\SampleService>

 

这样,就会在 J:\SampleService 目录下生成一个 SampleService.aar 包。

 

7.部署测试服务

 

第1种:使用管理控制台部署

打开管理控制台:http://localhost:8080/axis2/axis2-admin/

输入用户名密码:admin/axis2

选择第一项 : Upload Service

找到刚才生成的 SampleService.aar 直接部署吧(点击 浏览 --> upload)



第2种:手工拷贝到对应地址

这个没得说,就把 SampleService.aar 直接拷贝到

%TOMCAT_HOME%\webapps\axis2\WEB-INF\services

这个目录下面就可以了。

8.查看是否部署成功


http://localhost:8080/axis2/services/listServices

 

 打开后,可以看到:

UserGuideSampleService

多出了这么一个服务,说明部署成功。

 

9.编写客户端进行测试

 

在刚刚新建的java项目下:

在 mm.test.webservices.axis2userguide 包下

新建 : SampleClient.java  内容如下:

package mm.test.webservices.axis2userguide;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.Constants;
import org.apache.axis2.client.ServiceClient;

public class SampleClient {

	private static EndpointReference targetEPR = new EndpointReference(
			"http://localhost:8080/axis2/services/UserGuideSampleService");

	public static OMElement greetUserPayload(String personToGreet) {
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace(
				"http://example1.org/example1", "example1");
		OMElement method = fac.createOMElement("sayHello", omNs);
		OMElement value = fac.createOMElement("personToGreet", omNs);
		value.addChild(fac.createOMText(value, personToGreet));
		method.addChild(value);
		return method;
	}

	public static void main(String[] args) {
		try {
			OMElement payload = SampleClient.greetUserPayload("John");
			Options options = new Options();
			options.setTo(targetEPR);

			options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

			ServiceClient sender = new ServiceClient();
			sender.setOptions(options);
			OMElement result = sender.sendReceive(payload);

			String response = result.getFirstElement().getText();
			System.out.println(response);

		} catch (Exception e) { // (XMLStreamException e) {
			System.out.println(e.toString());
		}
	}

}

 

右键运行下,如果发现控制台输出:

 

Hello, John

 说明:调用服务成功。 

 

 

 

 

0
1
分享到:
评论

相关推荐

    axis2入门及简单例子

    axis2入门及简单例子

    axis2;WebService

    Axis的简单准备 Axis的入门实例 Axis复杂对象类型的WebService Axis的辅助工具发布、调用WebService Axis WebService会话Session的管理 Axis用控制台Dos命令发布WebService Axis跨多个WebService管理Session Axis用...

    axis2_WebService_开发指南

    Axis 的简单准备 Axis 的入门实例 Axis 复杂对象类型的WebService Axis 的辅助工具发布、调用WebService Axis WebService 会话Session 的管理 Axis 用控制台Dos 命令发布WebService Axis 跨多个WebService 管理...

    Axis2 WebService 开发指南 技术文档 入门文档

    Axis的简单准备 Axis的入门实例 Axis复杂对象类型的WebService Axis的辅助工具发布、调用WebService Axis WebService会话Session的管理 Axis用控制台Dos命令发布WebService Axis跨多个WebService管理Session ...

    axis入门到深入及完全部署手册

    axis使得webservice相当简单,半天就可以精通

    axis2_webservice

    Axis的简单准备 Axis的入门实例 Axis复杂对象类型的WebService Axis的辅助工具发布、调用WebService AxisWebService会话Session的管理 Axis用控制台Dos命令发布WebService Axis跨多个WebService管理Session Axis用...

    Axis WebService 技术指南,开发文档

    Axis的简单入门实例 Axis的jws发布WebService Axis的wsdd模式发布WebService Axis中传递复杂对象和内部类 Axis中的handler、chain的特性 Axis中处理异常信息 Axis中传递文件信息 Axis常用命令介绍 Axis 提供的工具和...

    axis2编写发布和例子详解

    axis2入门学习资料,通过10个步骤详细例子(测试都能运行),简单阐述了ajax通过tomcat发布方法,session管理,异步调用和同步调用,spring整合等功能,说明,配置比较详细

    WebService入门简单实例(采用AXIS的方式)

    NULL 博文链接:https://qiangjiyi.iteye.com/blog/2223655

    webservice的axis2开发

    学习webservice必不可少的资料,入门简单

    编程入门_Eclipse教程

    3.1. Axis2的简单WebService示例 10 3.1.1. 0配置POJO的Webservice 10 3.1.2. 用axis2的辅助工具发布、调用WebService 12 3.1.3. WebService会话Session的管理 24 3.1.4. 用Spring创建的JavaBean发布成WebService 29...

    Axis学习笔记(网页转贴)

    axis入门,简单实用 包括三个人的博客文章

    MyWebService源码(axis)

    自己写的一个简单的axis的java webservice,希望入门的学习者有用。

    java webservice Axis例子

    一个简单的Axis例子. eclipse工程代码,服务器端代码和客户端代码都有.能直接运行,对入门级的朋友来说不错.

    java webservice AXIS,XFire , CXF

    java webservice AXIS,XFire , CXF介绍,包含简单的CFX例子,对于初步接触、入门有帮助。

    TwinCAT2 运动控制入门教程

    如何在 System Manager 中独立硬件的 Axis 配置和调试界面;TwinCAT PLC Control 中独立于硬件的单轴及多轴运动控制程序的编写;完整的 AX5000 和电机参数的设置;以及电子凸轮表、飞锯、TwinCAT NC FIFO 等功能。

    Java的webservice开发与测试入门例子

    最简单的webservice的例子,包括服务端与客户端,有axis、xfire的两种方式的实现

    Eclipse开发WEBSERVICE示例HELLWORD源码

    Eclipse开发WEBSERVICE示例HELLWORD源码,最简单的入门示例,含client和Services端 1)导入ecplise中,eclipse需要先配置好tomcat+axis2环境; 2)用eclipse的 axis2插件菜单发布Hellword Service到tomcat中 3)...

    World Wind学习文档大全

    5、简单功能分析——截屏功能和“关于”窗体分析、功能分析——Show Planet Axis、Show Position 、Show Cross Hairs功能插件加载过程全解析 6、渲染过程解析篇 7、LoadUnload Plugins——投石问路篇 8、LoadUnload ...

Global site tag (gtag.js) - Google Analytics