参考资料:

http://blog.csdn.net/apei830/article/details/5448897

axis2的官网

http://axis.apache.org/axis2/java/core/docs/pojoguide.html

1、先来构建web端,搭建服务平台

a、从这 http://axis.apache.org/axis2/java/core/download.cgi 下载  axis2-1.6.2-war.zip

然后将里面的 axis2-web文件夹复制到你新建的java web的WerRoot下面

axis2\WEB-INF\lib包里面的jar复制到你的lib下

b、然后配置你的web.xml文件 加上AxisServlet配置 并且修改你项目的首页

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>/axis2-web/index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping> </web-app>

c、构建你对外发布的服务 这里测试用的 如下  包含对外两个方法

一个输入文本 返回文本

一个上传二进制文件 返回布尔值

package com.undergrowth.ws.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import javax.activation.DataHandler; public class WSTest { public String sayHello(String name){
return name+"\tHello World\t"+"WebServices Axis2";
} public boolean uploadWithDataHandle(DataHandler dHandler,String fileName){ FileOutputStream fos=null;
try {
File file=new File("d:/upload_res/");
if(!file.exists()) file.mkdir();
fos=new FileOutputStream(new File(file,fileName));
writeFileToOutput(fos,dHandler.getInputStream());
System.out.println("上传成功!");
fos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} return true;
} private void writeFileToOutput(FileOutputStream fos, InputStream inputStream) throws IOException {
// TODO Auto-generated method stub
int n=0;
byte[] data=new byte[1024*8];
while((n=inputStream.read(data))!=-1){
fos.write(data,0,n);
}
} }

d、向AXIS2的框架描述你要提供的服务  即services.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="WSTest">
<description>Web Service例子</description>
<parameter name="ServiceClass">com.undergrowth.ws.test.WSTest</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>
</serviceGroup>

对于services.xml文件的位置 只用放在你项目的web-inf下面即可  当然还有很多种放置方式

对于services.xml的文件位置 我查看了AsixServlet源码  有如下这么一段 关于services.xml的放置位置的

当然中间经过很多转换 最终是在  WarBasedAxisConfigurator类下有这么一个方法

public void loadServices() {
String repository = config.getInitParameter("axis2.repository.path");
if (repository != null) {
super.loadServices();
log.debug((new StringBuilder()).append(
"loaded services from path: ").append(repository)
.toString());
return;
}
String repository = config.getInitParameter("axis2.repository.url");
if (repository != null) {
loadServicesFromUrl(new URL(repository));
log.debug((new StringBuilder())
.append("loaded services from URL: ").append(repository)
.toString());
return;
}
loadServicesFromWebInf();
if (config.getServletContext().getRealPath("") != null
|| config.getServletContext().getRealPath("/") != null) {
super.loadServices();
log.debug("loaded services from webapp");
return;
}
try {
URL url = config.getServletContext().getResource("/WEB-INF/");
if (url != null) {
loadServicesFromUrl(url);
log.debug("loaded services from /WEB-INF/ folder (URL)");
}
} catch (MalformedURLException e) {
log.info(e.getMessage());
}
return;
}

接下来有一个方法

private void loadServicesFromWebInf() {
try {
InputStream servicexml = config.getServletContext()
.getResourceAsStream("/WEB-INF/services.xml");
if (servicexml != null) {
HashMap wsdlServices = new HashMap();
ArchiveReader archiveReader = new ArchiveReader();
String path = config.getServletContext()
.getRealPath("/WEB-INF");
if (path != null)
archiveReader.processFilesInFolder(new File(path),
wsdlServices);
org.apache.axis2.description.AxisServiceGroup serviceGroup = DeploymentEngine
.buildServiceGroup(servicexml, Thread.currentThread()
.getContextClassLoader(), "annonServiceGroup",
configContext, archiveReader, wsdlServices);
axisConfig.addServiceGroup(serviceGroup);
}
} catch (AxisFault axisFault) {
log.info(axisFault);
} catch (FileNotFoundException e) {
log.info(e);
} catch (XMLStreamException e) {
log.info(e);
}
}

看上面那个方法  就是从web-inf下面获取services.xml文件啊  然后调用

axisConfig.addServiceGroup(serviceGroup);

添加axisConfig.addServiceGroup  添加ajaxService

附  目录结构如下:

e、经过上面几步  发布项目  测试

http://localhost:8888/Axis2Web/

http://localhost:8888/Axis2Web/services/WSTest?wsdl

2、构建客户端 客户端也有很多中方式 这里采用 RPCServiceClient 进行调用

调用HelloWorld

package com.undergrowth.webservices;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.rpc.client.RPCServiceClient; public class RPCClientHelloWorld { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub try {
//1.创建webServices客户端
RPCServiceClient rpClient=new RPCServiceClient();
rpClient.getOptions().setTo(new EndpointReference("http://localhost:8888/Axis2Web/services/WSTest"));
//2.设置调用参数
QName opEntryQNama=new QName("http://test.ws.undergrowth.com","sayHello");
//输入参数
Object[] opEntryArgs=new Object[]{"google"};
//返回参数
Class[] returnClassArgs=new Class[]{String.class};
//3,进行调用
System.out.println(rpClient.invokeBlocking(opEntryQNama, opEntryArgs, returnClassArgs)[0]);
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

调用文件上传

package com.undergrowth.webservices;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName; import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.rpc.client.RPCServiceClient; public class RPCClientUploadFile { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub try {
//1.创建webServices客户端
RPCServiceClient rpClient=new RPCServiceClient();
rpClient.getOptions().setTo(new EndpointReference("http://localhost:8888/Axis2Web/services/WSTest"));
//2.设置调用参数
QName opEntryQNama=new QName("http://test.ws.undergrowth.com","uploadWithDataHandle");
//输入参数
Object[] opEntryArgs=new Object[]{new DataHandler(new FileDataSource("/csg.jpg")),"csg.jpg"};
//返回参数
Class[] returnClassArgs=new Class[]{Boolean.class};
//3,进行调用
System.out.println("上传结果为:"+rpClient.invokeBlocking(opEntryQNama, opEntryArgs, returnClassArgs)[0]);
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

好了 记录学习的脚步

axis2_1.6.2之构建web端和客户端 .的更多相关文章

  1. 从零开始搭建Electron+Vue+Webpack项目框架,一套代码,同时构建客户端、web端(一)

    摘要:随着前端技术的飞速发展,越来越多的技术领域开始被前端工程师踏足.从NodeJs问世至今,各种前端工具脚手架.服务端框架层出不穷,“全栈工程师”对于前端开发者来说,再也不只是说说而已.在NodeJ ...

  2. 【原创】从零开始搭建Electron+Vue+Webpack项目框架(六)Electron打包,同时构建客户端和web端

    导航: (一)Electron跑起来(二)从零搭建Vue全家桶+webpack项目框架(三)Electron+Vue+Webpack,联合调试整个项目(四)Electron配置润色(五)预加载及自动更 ...

  3. 《深入浅出Node.js》第8章 构建Web应用

    @by Ruth92(转载请注明出处) 第8章 构建Web应用 一.基础功能 请求方法:GET.POST.HEAD.DELETE.PUT.CONNECT GET /path?foo=bar HTTP/ ...

  4. Asp.net SignalR 实现服务端消息推送到Web端

              之前的文章介绍过Asp.net SignalR,  ASP .NET SignalR是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信.  今天我 ...

  5. Node.js高级编程读书笔记 - 4 构建Web应用程序

    Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ...

  6. Comet技术详解:基于HTTP长连接的Web端实时通信技术

    前言 一般来说,Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Ser ...

  7. 高效构建Web应用 教你玩转Play框架 http://www.anool.net/?p=577

    Play 框架是一个完整的Web应用开发框架,覆盖了Web应用开发的各个方面.Play 框架在设计的时候借鉴了流行的 Ruby on Rails 和 Grails 等框架,又有自己独有的优势.使用 P ...

  8. 使用XFire+Spring构建Web Service(一)——helloWorld篇

    转自:http://www.blogjava.net/amigoxie/archive/2007/09/26/148207.html原文出处:http://tech.it168.com/j/2007- ...

  9. 使用XFire+Spring构建Web Service

    XFire是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地开发Web Service应用. 相 对于Axis来说,目 ...

随机推荐

  1. thinkphp模板中foreach循环没数据的错误解决

    从控制器方法中$this->assign();函数将值传递给html模板 但是模板不显示数据,直接出来的是代码,效果就和html中写了php代码不能解析一样. 原来是我将thinkphp框架的引 ...

  2. HDU 1069 Monkey and Banana(二维偏序LIS的应用)

    ---恢复内容开始--- Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  3. [办公应用]我的WORD文档表格操作不灵活 无法调整列宽

    最近同事的一个word文档中的表格操作非常不灵活,用鼠标直接调整列宽时总觉得很不灵活.她的操作系统为XP,office 为微软office 2003. 我首先检查了木马,检查了输入法等,结果都没有问题 ...

  4. FAQ软件卸载

    今天安装了一个PDF 编辑软件pdftk,下载时提示有50多M,感觉不好用,卸载后提示释放空间只有2M,郁闷了.上网查寻,如下命令 sudo dpkg -p package_name卸载软件包及其配置 ...

  5. 利用crontab定时重启centos

    起因 前一段买了aliyun的ecs的最低配版,大概配置是centos 7,512内存,20G空间. 部署了几个站点,虽然网站已经做了一定的静态化,但还是会出现内存不够用的情况,这个时候,系统会停掉一 ...

  6. Oracle Merge Into 用法详解

    原文:http://blog.csdn.net/EdgenHuang/article/details/3587912 Oracle9i引入了MERGE命令,你能够在一个SQL语句中对一个表同时执行in ...

  7. FileResult,JavaScriptResult,JsonResult

    FileResult:可以响应任意文档的属性,包括二进制格式的数据,eg:图档,pdf,excel,zip,可以传入byte[],文档路径,Stream等不同的属性,让mvc将属性回传给客户端,除此之 ...

  8. laravel 取sql语句

    \DB::connection()->enableQueryLog(); some sql action... $query = \DB::getQueryLog(); $lastQuery = ...

  9. 7添加一个“X”到HTML:转到XHTML

    XHTML中的X代表extensible,是以XML为基础的另一种说法.XML表示可扩展的标记语言. XML是一种可以用来开发新的标记语言的语言,而HTML只是一门标记语言. HTML转化为XHTML ...

  10. 数据库主键跟外键+修改mysql的密码

    update myspl.user set password=PASSWORD(设置的密码)  where user='root'; 如果修改错误:先执行use mysple;再重复上面的代码. 一. ...