【原创】axis基本操作文档

1. 环境准备

Tomcat5.0;jdk1.5;axis1.4;

2. Axis包介绍

以上就是下载axis2之后的目录结构,大体介绍一下:

Docs: 关于axis2日常使用方法,用户指导等都在这个目录。

Lib: 开发使用的.jar可以从这里拿到。

Samples:顾名思义,里边都是你需要的例子。

Webapps:我们使用的axis产品就在这里。

Xmls: 平时没有用过,查了一些资料,好像是使用ANT时的一些配置文件。

3. Axis安装

Axis安装很简单,把axis1_4/webapps下的axiscopytamcat的部署目录中即可。

验证axis是否安装成功,可以在IE地址栏中输入:http://10.10.164.199:8080/axis/

出现以上页面即表示axis部署成功,但还需要校验一下axis的组件是否安装完整,点击“Validation” ,如果出现以下页面即表示所有组件安装成功。

Axis安装已经完成,现在进入开发阶段。

4. 进入开发

Axis的开发需要准备以下内容,

服务类的开发;

服务类部署配置文件,用于生成server_config.wsdl;

执行部署文件的脚本,以下是个例子;

客户端程序;

以下例子也都是按照以上内容逐个提供。

a)  基本数据类型传输

(1)服务类

package service;

public class TestService {

public  String sayHello(String argv){

System.out.println(" say Hello to "+argv);

return " say Hello to "+argv;

}

/**

 * @param args

 */

public static void main(String[] args) {

TestService t = new TestService();

t.sayHello("yaa") ;

}

}

(2)服务类配置文件

  

  

 

(3)以上已经提供

(4)客户端程序

package client;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

public class ClientTest {

/**

 * @param args

 */

public static void main(String[] args) {

try{

String url ="http://localhost:8080/axis/services/Test" ;

Service server=  new Service() ;

Call call = (Call)server.createCall() ;

call.setTargetEndpointAddress(url) ;

call.setOperationName(new QName(url,"sayHello")) ;

String result = (String)call.invoke(new Object[]{"huoch"}) ;

System.out.println("执行结果 : "+result);

}catch(Exception ex){

ex.printStackTrace() ;

}

}

}

b)  POJO自定义类传输

        (1)服务类以及POJO

Person.java: 

package service;

import java.io.Serializable;

public class Person implements Serializable {

private String id ;

private String name ;

private String remark ;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getRemark() {

return remark;

}

public void setRemark(String remark) {

this.remark = remark;

}

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

ObjectServer.java:(此服务类提供了POJO对象传输和POJO对象数组的传递)

package service;

import java.util.ArrayList;

public class ObjectService {

//传递对象

public Person getPerson(){

Person person  = new Person() ;

person.setId("1");

person.setName("huoch");

return person ;

}

//传递对象数组

public Person[] getPersons(){

Person[] persons= new Person[3];

int i=0;

for(i=0;i<3 ;i++){

Person a = new Person() ;

a.setId(new Integer(i).toString()) ;

a.setName("huoch"+new Integer(i).toString()) ;

persons[i]=a;

}

return persons ;

}

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

   (2)服务类配置文件(注意其中对POJO对象的配置和对象数组的配置)

  

  

  <!--

  

  

  --&gt

   

       xmlns:ns="http://wxm.webservice"

       qname="ns:Person"

       type="java:service.Person"

       serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"

       deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"

       encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

     />

      

       

       xmlns:ns="http://wxm.webservice"

       qname="ns:PersonArray"

       type="java:service.Person[]"

       serializer="org.apache.axis.encoding.ser.ArraySerializerFactory"

       deserializer="org.apache.axis.encoding.ser.ArrayDeserializerFactory"

       encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

     /> 

   (3)以上已经提供

    (4)客户端程序

package client;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.encoding.ser.BeanDeserializerFactory;

import org.apache.axis.encoding.ser.BeanSerializerFactory;

import service.Person;

public class ObjectTestClient {

/**

 * @param args

 */

public static void main(String[] args) {

try{

String url ="http://localhost:8080/axis/services/ObjectServer" ;

Service server=  new Service() ;

Call call = (Call)server.createCall() ;

//如果要传递对象,对象必须是可序列化的

//传递对象时wsdd中要新建beanmapping节点

//传递对象时客户端要以qname的形式注册对象---其实就是指定一种类型

//传递对象时客户端要指定返回对象类型

QName qname = new QName("urn:ObjectService","Person");  

call.registerTypeMapping(Person.class, qname, new BeanSerializerFactory(Person.class,qname),new BeanDeserializerFactory(Person.class,qname)) ;

call.setTargetEndpointAddress(url) ;

call.setOperationName(new QName("ObjectServer","getPerson")) ;

call.setReturnClass(Person.class) ;

Object[] a = null ;

Person result = (Person)call.invoke(a) ;

System.out.println("执行结果 :客户id: "+result.getId() +"  客户姓名 :"+result.getName());

System.out.println("开始调用数组+++++++++++++++++++++++++++");

QName qn = new QName("urn:ObjectService","PersonArray");  

call.registerTypeMapping(Person[].class, qn, new BeanSerializerFactory(Person[].class,qn),new BeanDeserializerFactory(Person[].class,qn)) ;

call.setTargetEndpointAddress(url) ;

call.setOperationName(new QName("ObjectServer","getPersons")) ;

call.setReturnClass(Person[].class) ;

Object[] b=null ;

Person[] result2 = (Person[]) call.invoke(b) ;

for (int i=0 ;i

System.out.println("数组"+i+" : "+result2[i].getName());

}

}catch(Exception ex){

ex.printStackTrace() ;

}

}

}

c)  XML传输

(1)服务类

MessageServer.java :

package service;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.soap.SOAPException;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Result;

import javax.xml.transform.Source;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.apache.axis.AxisFault;

import org.apache.axis.message.SOAPFault;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class MessageService {

/**

 * 通过传递和接收Element[]的方式传递xml 信息

 * @param e

 * @return

 * @throws Exception 

 */

public Element[] MessageService(Element[] argv)throws SOAPException {

Element[] e_array = null ;

try{

//   int a = 10/0 ;

e_array = argv ;

Element  e = e_array[0] ;

NodeList nodelist = e.getChildNodes();

for (int i=0;i

Node item = nodelist.item(i) ;

if(item.getChildNodes().getLength()==1){

System.out.println("节点 "+item.getNodeName()+": "+item.getTextContent());

}else{

for(int j=0 ;j

{

Node item1 = item.getChildNodes().item(j) ;

System.out.println("节点 "+item1.getNodeName()+": "+item1.getTextContent());

}

}

}

//将xml保存到文件中----------------------------------------

System.out.println("开始打印xml结构");

Source v_xmlSource = new DOMSource(e);

    ByteArrayOutputStream v_baos = new ByteArrayOutputStream();

    Result v_result = new StreamResult(v_baos);

    Transformer v_transformer=null;

    try {

      TransformerFactory v_transFactory = TransformerFactory

          .newInstance();

      v_transformer = v_transFactory.newTransformer();

    }

    catch (Exception exx) {

      exx.printStackTrace();

    }

    v_transformer.setOutputProperty(OutputKeys.METHOD, "xml");

    v_transformer.setOutputProperty(OutputKeys.ENCODING, "GBK");

    // Transform the Document

  

      v_transformer.transform(v_xmlSource, v_result);

      v_baos.close();

    

 

    System.out.println("xml 内容: "+new String(v_baos.toByteArray()));

 

 

}catch(Exception ex){

SOAPFault sf = new SOAPFault(new AxisFault(ex.getMessage())) ;

e_array[0]=sf;

}

   return e_array ;

}

/**

 * @param args

 */

public static void main(String[] args) {

try{

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance() ;

DocumentBuilder db = dbf.newDocumentBuilder() ;

Document doc = db.parse("src/service/test.xml") ;

NodeList nodelist =  doc.getChildNodes() ;

for (int i=0;i

Node item = nodelist.item(i);

System.out.println("节点 "+item.getNodeName() +" : "+item.getNodeValue()) ;

}

Element[] Element =new Element[1] ;

Element[0]=(Element)doc.getFirstChild() ;

NodeList nodelist2 = Element[0].getChildNodes() ;

System.out.println(" nodelist.length  :"+nodelist2.getLength());

for(int i=0;i

Node item = nodelist2.item(i) ;

System.out.println("第一节点下内容: "+item.getNodeName()+i+" : "+item.getTextContent()) ;

}

//调用服务类

System.out.println("调用服务类");

MessageService ms = new MessageService () ;

Element[] result = ms.MessageService(Element) ;

System.out.println("最终结果:"+result[0].getNodeName());

}catch(Exception e){

e.printStackTrace() ;

}

}

}

     (2)服务类配置文件

  

  

  

   

 

     (3)以上已经提供

     (4)客户端程序

package client;

import java.util.Iterator;

import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.soap.SOAPElement;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.message.SOAPBodyElement;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import com.sun.org.apache.xerces.internal.xni.QName;

public class MessageTestClient {

/**

 * @param args

 */

public static void main(String[] args) {

try{

Service service = new Service() ;

Call call = (Call)service.createCall() ;

String url = "http://localhost:8082/axis/services/MessageService" ;

call.setTargetEndpointAddress(url) ;

//创建w3c xml

System.out.println("创建xml.....") ;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance() ;

DocumentBuilder db = dbf.newDocumentBuilder() ;

Document doc = db.parse("src/service/test.xml") ;

Element[] Element =new Element[1] ;

Element[0]=(Element)doc.getFirstChild() ;

SOAPBodyElement[] soapelement = new SOAPBodyElement[1] ;

soapelement[0] = new SOAPBodyElement(Element[0]) ;

//调用服务

System.out.println("开始调用服务........");

call.setOperationName(new javax.xml.namespace.QName(url,"MessageService")) ;

Vector result =(Vector) call.invoke(soapelement) ;

System.out.println("调用服务结束,打印信息..............");

Iterator iterator = result.iterator();

while(iterator.hasNext()){

SOAPBodyElement resultelement=(SOAPBodyElement) iterator.next() ;

System.out.println("调用服务后节点 "+resultelement.getNodeName()+" : "+resultelement.getObjectValue()) ;

}

}catch(Exception ex ){

ex.printStackTrace() ;

}

}

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7389553/viewspace-673079/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7389553/viewspace-673079/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值