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命令。】

效果如下:

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

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

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

二、创建一个简单的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链接,如果出现下图,表明服务发布成功。

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

点击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

相关推荐