`
bcworld
  • 浏览: 37817 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

axis2创建,部署,发布和访问web service

阅读更多

使用axis2创建web service是一种常用的发布web服务的方式,下面从搭建axis2开发环境开始,写一个简单的web服务,然后再写一个客户端程序,访问我们刚才创建的WEB 服务。

一、搭建axis2环境

  1.到apache官网下载:http://axis.apache.org/axis2/java/core/download.cgi

  2.解压下载到的ZIP包

  3.集成到TOMCAT上:

   运行CMD,切换目录到解压包的webapp目录下,该目录下有一个build.xml,运行ant命令,完成之后会在webapp同级目录中生成一个 dist目录,将该目录下的axis2.war拷贝到TOMCAT下的webapp目录下,当启动TOMCAT时,会自动解压axis2.war,生成 axis2文件夹,axis2/WEB-INF/services 目录很重要,以后的web服务都是部署在这个目录下面的。

【如果没有ANT,可以到apache官网下载:http://ant.apache.org/bindownload.cgi ,完成后可将解压后的bin目录添加到系统变量path中,这样方便在任意的地方运行ant命令。】

效果如下:

 

 

4.启动TOMCAT服务器,在浏览器中输入http://localhost:8080/axis2/,如果出现下图,则表明环境OK。

二、创建一个简单的web服务

  1.服务代码如下:

package samples.quickstart.service.pojo;
import java.util.HashMap;
public class StockQuoteService {
    private HashMap map = new HashMap();
    public double getPrice(String symbol) {
        Double price = (Double) map.get(symbol);
        if(price != null){
            return price.doubleValue();
        }
        return 42.00;
    }
    public void update(String symbol, double price) {
        map.put(symbol, new Double(price));
    }
}

 2.写完服务代码还需要用XML配置该服务,如下:

<service name="StockQuoteService" scope="application" targetNamespace="http://quickstart.samples/">
    <description>
        Stock Quote Service
    </description>
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                         class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>
    <schema schemaNamespace="http://quickstart.samples/xsd"/>
    <parameter name="ServiceClass">samples.quickstart.service.pojo.StockQuoteService</parameter>
</service>

 3.部署服务:

  到部署目录下,新建一个文件夹如myfirstservice(名称没有要求),然后再其下新建META-INF 目录,把刚才的服务配置services.xml放到该目录下,然后把含包结构的class文件放到部署目录下,即可完成部署。

axis2/WEB-INF/services

                               -myfirstservice

                                      -META-INF

                                                -services.xml

                                      -samples

                                                -quickstart

                                                         -service

                                                                -pojo

                                                                     -StockQuoteService.class

4.重启TOMCAT,输入http://localhost:8080/axis2/,点击Services链接,如果出现下图,表明服务发布成功。

点击StockQuoteService可以看到该服务的WSDL:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://quickstart.samples/xsd" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:tns="http://quickstart.samples/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://quickstart.samples/">
  <wsdl:documentation>StockQuoteService</wsdl:documentation> 
- <wsdl:types>
- <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd">
- <xs:element name="update">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="symbol" nillable="true" type="xs:string" /> 
  <xs:element minOccurs="0" name="price" type="xs:double" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="getPrice">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="symbol" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="getPriceResponse">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="return" type="xs:double" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>
  </wsdl:types>
- <wsdl:message name="getPriceRequest">
  <wsdl:part name="parameters" element="ns:getPrice" /> 
  </wsdl:message>
- <wsdl:message name="getPriceResponse">
  <wsdl:part name="parameters" element="ns:getPriceResponse" /> 
  </wsdl:message>
- <wsdl:message name="updateRequest">
  <wsdl:part name="parameters" element="ns:update" /> 
  </wsdl:message>
- <wsdl:portType name="StockQuoteServicePortType">
- <wsdl:operation name="getPrice">
  <wsdl:input message="tns:getPriceRequest" wsaw:Action="urn:getPrice" /> 
  <wsdl:output message="tns:getPriceResponse" wsaw:Action="urn:getPriceResponse" /> 
  </wsdl:operation>
- <wsdl:operation name="update">
  <wsdl:input message="tns:updateRequest" wsaw:Action="urn:update" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="StockQuoteServiceSoap11Binding" type="tns:StockQuoteServicePortType">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="getPrice">
  <soap:operation soapAction="urn:getPrice" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="update">
  <soap:operation soapAction="urn:update" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="StockQuoteServiceSoap12Binding" type="tns:StockQuoteServicePortType">
  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="getPrice">
  <soap12:operation soapAction="urn:getPrice" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap12:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="update">
  <soap12:operation soapAction="urn:update" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="StockQuoteServiceHttpBinding" type="tns:StockQuoteServicePortType">
  <http:binding verb="POST" /> 
- <wsdl:operation name="getPrice">
  <http:operation location="getPrice" /> 
- <wsdl:input>
  <mime:content type="text/xml" part="parameters" /> 
  </wsdl:input>
- <wsdl:output>
  <mime:content type="text/xml" part="parameters" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="update">
  <http:operation location="update" /> 
- <wsdl:input>
  <mime:content type="text/xml" part="parameters" /> 
  </wsdl:input>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="StockQuoteService">
- <wsdl:port name="StockQuoteServiceHttpSoap11Endpoint" binding="tns:StockQuoteServiceSoap11Binding">
  <soap:address location="http://localhost:8080/axis2/services/StockQuoteService.StockQuoteServiceHttpSoap11Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="StockQuoteServiceHttpSoap12Endpoint" binding="tns:StockQuoteServiceSoap12Binding">
  <soap12:address location="http://localhost:8080/axis2/services/StockQuoteService.StockQuoteServiceHttpSoap12Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="StockQuoteServiceHttpEndpoint" binding="tns:StockQuoteServiceHttpBinding">
  <http:address location="http://localhost:8080/axis2/services/StockQuoteService.StockQuoteServiceHttpEndpoint/" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

 

 这个WSDL文件对我们写客户端程序访问WEB服务非常有用,后面会用到。

三、部署WEB服务

  阅读上面的WSDL文件,需要得到关键的几个量,如访问地址,请求结构等。

  客户端代码如下:

package samples.quickstart.service.pojo;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
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.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class StockQuoteServiceClient {
	private static String toEpr = "http://localhost:8080/axis2/services/StockQuoteService.StockQuoteServiceHttpSoap12Endpoint/";
	public static void main(String[] args) throws Exception {
		Options options = new Options();
		options.setTo(new EndpointReference(toEpr));
		options.setAction("urn:update");
		//options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
		//options.setProperty(Constants.Configuration.ENABLE_REST,Constants.VALUE_TRUE);
		ServiceClient clientSender = new ServiceClient();
		//sender.engageModule(Constants.MODULE_ADDRESSING);
		clientSender.setOptions(options);
		//先设置一个值
		clientSender.sendRobust(getUpdateLoad());
		//再获取该值
		options.setAction("urn:getPrice");
		OMElement result = clientSender.sendReceive(getPriceload());
		//打印完整的response
		System.out.println(result);
		OMElement returnElement=result.getFirstElement();
		System.out.println("The price is "+returnElement.getText());
//		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 getUpdateLoad() {
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace(
				"http://quickstart.samples/xsd", "example1");
		OMElement update=fac.createOMElement("update",omNs);
		OMElement symbol = fac.createOMElement("symbol", omNs);
			symbol.setText("yuan");
		OMElement price = fac.createOMElement("price", omNs);
			price.setText("12358.421");
		update.addChild(symbol);
		update.addChild(price);
		return update;
	}
	private static OMElement getPriceload() {
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace(
				"http://quickstart.samples/xsd", "example1");
		OMElement getPrice=fac.createOMElement("getPrice",omNs);
		OMElement symbol = fac.createOMElement("symbol", omNs);
			symbol.setText("yuan");
		getPrice.addChild(symbol);
		return getPrice;
	}
}

 

 程序运行结果如下:

log4j:WARN No appenders could be found for logger (org.apache.axis2.client.Options).
log4j:WARN Please initialize the log4j system properly.

<ns:getPriceResponse xmlns:ns="http://quickstart.samples/xsd"><ns:return>12358.421</ns:return></ns:getPriceResponse>
The price is 12358.421

  • 大小: 31.4 KB
  • 大小: 44.8 KB
  • 大小: 7.4 KB
分享到:
评论

相关推荐

    Axis通过wsdd部署Web Service

    通过一个实例来学习Axis通过wsdd部署Web Service

    Axis2快速构建Web Service ppt

    .Web Service简介 .如何开发一个Web Service .Axis2简介 .Axis2安装与部署 .Web Service开发步骤

    axis web service例子

    本案例为java开发的axis的web service案例。下载后直接部署到tomcat下即可运行。大家互相学习。

    部署axis2c的文档

    axis2c的在windows上的安装操作步骤

    基于WAS CE和Axis2开发Web Service应用

    随着Web Service技术迅速发展,基于Web Service开发的应用被使用...本文将介绍如何使用WAS CE(WebSphere Application Server Community Edition)和Apache Axis2开发、部署及测试一个简单的Web Service应用-网上花店。

    Axis2运行环境2

    1. axis2-1.1.1.rar中包含工程中所需的axis2的类包。 2. axis2.war是打包好的web应用。用于放在Tomcat中,当运行Tomcat后会自动解压。...具体的部署步骤和使用请参照《(原创)用Eclipse + Axis2+Tomcat创建Web Service》

    Axis2运行环境1

    1. axis2-1.1.1.rar中包含工程中所需的axis2的类包。 2. axis2.war是打包好的web应用。用于放在Tomcat中,当运行Tomcat后会自动解压。...具体的部署步骤和使用请参照《(原创)用Eclipse + Axis2+Tomcat创建Web Service》

    MyEclipse 开发部署 Web Service

    适用初学web service。采用xfire 和 axis 客户端调用

    用AXIS2开发WebService

    1. AXIS2简介 本文介绍如何在eclipse3.2下用Axis2开发web service,并将Axis2与自己的web service部署在Tomcat 6.0.18下,再通过在C#下写测试程序,来访问Tomcat下的web service。

    AXIS2代码示例详解-HelloWorld

    文将介绍如何使用Tomcat5.0和Apache Axis2开发、部署及测试一个简单的Web Service应用

    Java Web Service总结

    最近学习Web Service的总结,主要是使用axis实现Web Service,其中有详细的用eclipse来完成Web Service的部署,以及生产客户端的代码。。。 好东西要大家分享哦....

    axis开发资料

    项目收集的axis的相关资料~~ Axis.pdf AXIS(Java+WebSerivce)全攻略.mht axis开发.doc 用Axis开发基于Java的Web服务....使用Apache Axis部署 Web服务时的常见问题及其解决方法.txt 使用Axis开发Web Service程序.txt

    编程入门_Eclipse教程

    2.1. Axis2下载,并部署axis2到Tomcat 7 2.2. Eclipse下安装Axis2插件 8 2.2.1. 下载axis2的eclipse插件 8 3. 编写Web service程序 10 3.1. Axis2的简单WebService示例 10 3.1.1. 0配置POJO的Webservice 10 3.1.2. ...

    axis(里面包含整体的axis需要的JAR包,不需要另外下载)

    在部署web service服务的时候,为了找一个axis,下载了许多jar包,jar包不好找,经过一天的努力,把所有的axis需要用的东东全打包在这里了。

    xml执行java源码-Axis-1.4-RCE-Poc:Axis&lt;=1.4远程命令执行(RCE)POC

    xml 执行java源码 Axis &lt;=1.4 远程命令执行(RCE) .../axis/services/AdminService HTTP/1.1 Host: localhost:8080 Connection: close Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*

    webService接口配置以及接口访问实例

    基于idea开发的简单webservice接口实例,test1为服务端配置了...test2为客户端,通过AXIS方式访问接口,Soap方式服务端能接收到请求信息,但客户端返回信息警告,有兴趣的可以研究一下;带AXIS和soap所需jar包

    致远A8-M平台API参考手册

    A8 平台提供基于 axis2 1.4 实现的标准 Web service。可以采取多种语言多种方式进行客户端开发。 本指南提供了 Java 和 C#的开发步骤,PHP 建议使用 WSO2 Web Services Framework for PHP 开发。 本指南所有 Java ...

    axise入门文档.rar

    1. 下载Axis,下载地址...建立一个run,在Main Class中输入:org.apache.axis.client.AdminClient,这个是用来部署Web Service的。在arguments中输入-lhttp://localhost/axis/services/AdminService src/deploy.wsdd

    WebService

    基于Axis2的Web Service的部署实例。

    Mule是一个企业服务总线(ESB)消息框架

    4.支持Axis或者Glue的Web Service. 5.灵活的部署结构[Topologies]包括Client/Server, P2P, ESB 和Enterprise Service Network. 6.与Spring 框架集成:可用作ESB 容器,也可以很容易的嵌入到Spring应用中. 7.使用...

Global site tag (gtag.js) - Google Analytics