• 2.2 Apache Axis2 快速学习手册之 AXIOM 构建 Web Service


    和上一篇的POJO 部署相比主要是services.xml 中配置的消息接受处理器类不一样和Java 类中写法不一样。

    使用AXIOM构建服务

    样例源码路径: C:Appsaxis2axis2-1.7.9samplesquickstartaxiom

    请注意/ samples / quickstartaxiom中包含的目录结构:

    - quickstartaxiom
       - README.txt
       - build.xml
       - resources
         - META-INF
           - services.xml
           - StockQuoteService.wsdl
       - src
         - samples
           - quickstart
             - service
               - axiom
                 - StockQuoteService.java
             - clients
               - AXIOMClient.java

    由于AXIOM略有不同,因此您需要一个与POJO不同的service.xml文件。定义它,如代码清单4所示。

    代码4:服务定义文件。

    <service name="StockQuoteService" scope="application">
        <description>
            Stock Quote Service
        </description>
        <operation name="getPrice">
            <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
        </operation>
        <operation name="update">
            <messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
        </operation>
        <parameter name="ServiceClass">samples.quickstart.service.axiom.StockQuoteService</parameter>
    </service>

    请注意,它几乎相同,只是在service.xml文件中显式定义了操作,MessageReceivers现在是RawXML。

    现在,上面引用的StockQuoteService.java类是一个使用Axis2库中的类的普通Java类,其定义如代码清单5所示。

    代码5:使用AXIOM的StockQuoteService类

    package samples.quickstart.service.axiom;
    
    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;
    
    import java.util.HashMap;
    public class StockQuoteService {
        private HashMap map = new HashMap();
    
        public OMElement getPrice(OMElement element) throws XMLStreamException {
            element.build();
            element.detach();
    
            OMElement symbolElement = element.getFirstElement();
            String symbol = symbolElement.getText();
    
            String returnText = "42";
            Double price = (Double) map.get(symbol);
            if(price != null){
                returnText  = "" + price.doubleValue();
            }
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace omNs =
                fac.createOMNamespace("http://axiom.service.quickstart.samples/xsd", "tns");
            OMElement method = fac.createOMElement("getPriceResponse", omNs);
            OMElement value = fac.createOMElement("price", omNs);
            value.addChild(fac.createOMText(value, returnText));
            method.addChild(value);
            return method;
        }
    
        public void update(OMElement element) throws XMLStreamException {
            element.build();
            element.detach();
    
            OMElement symbolElement = element.getFirstElement();
            String symbol = symbolElement.getText();
    
            OMElement priceElement = (OMElement)symbolElement.getNextOMSibling();
            String price = priceElement.getText();
    
            map.put(symbol, new Double(price));
        }
    }

    Axis2使用AXIOM,或AXIs对象模型,类似于DOM(文档对象模型)的结构,它基于StAX API(用于XML的Streaming API)。充当服务的方法必须将OMElement作为其参数,OMElement表示在这种情况下发生的XML元素是传入SOAP消息的有效负载。例如,方法getPrice(OMElement)提取有效负载元素的第一个子元素的内容,该元素对应于股票代码,并使用它来查找股票的当前价格。除非这是“仅在”服务,否则这些方法必须返回OMElement,因为它成为返回SOAP消息的有效负载。

    现在通过在Axis2_HOME / samples / quickstartaxiom目录中键入ant generate.service来构建项目。

    将StockQuoteService.aar文件放在servlet引擎的webapps / axis2 / WEB-INF / services目录中,并通过查看服务列表来检查是否已正确部署该服务,

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

     您还可以在以下位置检查自定义WSDL:

    http://localhost:8080/axis2/services/StockQuoteService?wsdl

     the schema

    http://localhost:8080/axis2/services/StockQuoteService?xsd
     
  • 相关阅读:
    最佳买卖股票时期含冷冻期
    牛客网刷题笔记
    交换字符中的元素
    刷题总结
    牛客基础网刷题笔记
    买卖股票的最佳时机 II
    Solution -「51nod 1355」斐波那契的最小公倍数
    Solution -「51nod 1584」加权约数和
    Solution -「CF 1375G」Tree Modification
    Solution -「洛谷 P5787」「模板」二分图(线段树分治)
  • 原文地址:https://www.cnblogs.com/xingyunblog/p/10330203.html
Copyright © 2020-2023  润新知