簡體   English   中英

在AXIS2中返回自定義XML

[英]Return custom XML in AXIS2

我有一個帶有AXIS2的Web服務,該服務返回一個字符串。 AXIS2生成具有以下內容的XML:

<ns:return>

  String....

</ns:return>

問題是...我可以修改此XML結構嗎? 我需要返回一個復雜的XML,如果我將標簽放入字符串中,則解析器不會將其檢測為XML響應,而是像String一樣檢測到它。

謝謝

我在這里試圖解決的是我在本論壇上多次遇到的兩個主要問題,直到我找到解決方案之前,我個人都無法找到實現許多人可能一直在尋找的結果的方法。

1. eclipse向導生成的Web服務返回節點“ return”下的值(包括復雜對象); 問題是很多人正在尋找“如何擺脫這個節點” :)。 這個過程有點復雜,但是一旦完成,就很容易維護想要以自己的格式發送其Web服務答復的格式。

2.如何動態添加處理程序,以實現上述第一個目標。

服務方法中的第一個:

  1. 在您的服務方法中(假設您的服務方法名稱為doSomethingAndGiveResult()),獲得消息上下文=> MessageContext mc = MessageContext.getCurrentMessageContext();

請添加軟件包org.apache.axis2.context.MessageContext

  1. 獲取配置根上下文ConfigurationContext cc = mc.getRootContext(); ; ArrayList al = mc.getExecutionChain();

     AxisConfiguration ac = cc.getAxisConfiguration(); AxisOperation ao = mc.getAxisOperation(); 

盡管不需要達到我們的目標,但您可以按以下方式檢查軸操作。

    for(int m=0; m<al.size(); m++)
    System.out.println("Excution chain -> " + String.valueOf(m) + "  "+ al.get(m).toString()); 

            ArrayList<AxisOperation> copyOfAxisOperation = new ArrayList<AxisOperation>();
    // work only on the copy of the phaseOutFlow
    copyOfAxisOperation.addAll(ao.getPhasesOutFlow());
  1. 現在動態添加一個我們感興趣的軸相位處理程序-

     if(bRetVal == false) // Note: Need to set the handler only once { Iterator itr5 = copyOfAxisOperation.iterator(); FaltuHandler faltuhandler = new FaltuHandler(); while(itr5.hasNext()){ Phase aphase = (Phase) itr5.next(); String str = aphase.toString(); System.out.println("Phases from operation = " + str); if ("OperationOutPhase".equals(str)) { try{ aphase.setPhaseLast(faltuhandler); }catch (Exception somee) { System.out.println("Some exception occured while adding handler"); somee.printStackTrace(); } } } } 

請注意bRetVal布爾值,您需要將其設置在某個地方,以便下次不會再次調用此處理程序設置代碼。

  1. 假設您將從服務中返回一些對象。 我在這里給出了代碼的用法,以便您可以根據項目的需要以及要返回的內容進行選擇。

盡管我們將在處理程序中執行相同的操作; 如果我們要覆蓋返回XML的值或格式(特別是在此處解決此寫問題的問題)

這需要以正確的XML格式從此處返回並進行序列化

經過多次研究,我發現最好使用公理包。 對於我的項目需要合適的; 但是,您可以自由選擇包裹。 但是請注意,從我們的服務方法中,我們正在返回OMElement。 請添加軟件包org.apache.axiom.om.OMElement(以及OMFactory和OMNamespace)

    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("", "");
    OMElement exec = fac.createOMElement("mynode", omNs);

            OMElement lbtu = fac.createOMElement("givingSomeInstruction", null, mynode);
    lbtu.setText("doOpenABrowser");
    mynode.addChild(lbtu); 

            .......
            .......
            return mynode;
            // Your service method ends here

其次-我們需要編寫PhaltuHandler :)

  1. 在同一程序包(即您的處理程序方法所在的com.something.yourpackage)中創建一個類,並實現Handler-

     public class FaltuHandler implements Handler { ...... } 
  2. 覆蓋所有方法,並為您想要的任何內容添加代碼。 我直接從項目空間復制了一些部分; 添加/刪除可能與您的項目相關的內容

      @Override 

    公共無效cleanup(){

    }

    @Override public void flowComplete(MessageContext arg0){

    }

    @Override public HandlerDescription getHandlerDesc(){

      handlerDesc = new HandlerDescription(); handlerDesc.setHandler(this); handlerDesc.setName(this.getName()); return handlerDesc; 

    }

    @Override public String getName(){

     return "FaltuHandler"; 

    }

    @Override公共參數getParameter(String arg0){

     return null; 

    }

    @Override public void init(HandlerDescription arg0){//無需執行任何操作}

    @Override public InvocationResponse invoke(MessageContext arg0)引發AxisFault {

    //在這里,我們需要根據項目的特定需求進行一些討厭的工作。 我在這段代碼中解釋內聯。

    SOAPEnvelope env = arg0.getEnvelope(); SOAPBody spbd =(SOAPBody)env.getBody(); System.out.println(“從處理程序中打印SOAP主體:” + spbd.toString());

    迭代器iterator = spbd.getChildElements();

    而(iterator.hasNext()){

    OMElement元素=(OMElement)iterator.next();
    System.out.println(“從處理程序(頂部子元素)迭代->” + element.getLocalName());

      // This section depicts how you can override the attributes and // values that you have set from the handler;which is already // set as XML elements. And just before your XML going out // of your system. Suppose I have an element called myResponse // then I will check the same in the following way and possibly // change some of attributes if ("myResponse".equals(element.getLocalName())) { // adding the attributes pertaining to this packet; this section of // code addresses many other related queries around this topic OMFactory fac = OMAbstractFactory.getOMFactory(); OMAttribute attr = fac.createOMAttribute("myVersion", null, "1.0"); element.addAttribute(attr); attr = fac.createOMAttribute("myStatus", null, "OK"); element.addAttribute(attr); attr = fac.createOMAttribute("myID", null, "1234"); element.addAttribute(attr); } // Following portion has been kept for understanding of how attributes // can be accessed. Skip, if you don't want. According to my project // XML format I am processing here Iterator it1 = element.getAllAttributes(); int k = 1; while(it1.hasNext()) { OMAttributeImpl elem = (OMAttributeImpl) it1.next(); System.out.println("Attribute" + String.valueOf(k) + " => " + elem.getLocalName() + " Value => " + elem.getAttributeValue()); k++; } // Let see if we have any child node here; most important section for getting // rid of "return". You need to find our where that "return" appears in your // XML and accordingly need to take action. Iterator it2 = element.getChildElements(); int j = 1; while(it2.hasNext()) { OMElement elem1 = (OMElement) it2.next(); if ("return".equals(elem1.getLocalName())) { // this we need to do in order to get rid of "return" node that gets added // automatically by the axis framework which does not meet our need elem1.discard(); System.out.println("after detaching the 'return' node"); // do dirty work - since our discard will remove the entire node to be // parsed that has child node and elements. So, we will build the data // again as below OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("", ""); // remember we returned myNode from our service? OMElement mynode = fac.createOMElement("My-name-Amit", omNs); OMElement spcode = fac.createOMElement("givingSomeInstruction", null, mynode); spcode.setText("doOpenABrowser"); mynode.addChild(spcode); System.out.println("after doing the dirty work"); element.addChild(mynode); } } } return InvocationResponse.CONTINUE; 

    }

這就是您實現以下目標的全部:1.如何擺脫服務中的“ return”元素2.如何自定義從eclipse向導生成的Web服務中獲取的返回值/ XML動態地將處理程序添加到軸階段

盡管我嘗試刪除與項目相關的所有不必要的代碼; 但是無法完全刪除。 但是請保持評論原樣,以便我們可以理解為了實現我們最初的主要目標而需要做什么。 如果括號不匹配,請原諒。

謝謝阿米特

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM