我有一個帶有AXIS2的webservice,它返回一個字符串。 AXIS2生成XML:返回AXIS2中的自定義XML
<ns:return>
String....
</ns:return>
問題是......我可以修改這個XML結構嗎?我需要返回一個複雜的XML,如果我把這些標籤放在字符串中,解析器不會將它檢測爲XML響應,它會像String一樣檢測它。
感謝
我有一個帶有AXIS2的webservice,它返回一個字符串。 AXIS2生成XML:返回AXIS2中的自定義XML
<ns:return>
String....
</ns:return>
問題是......我可以修改這個XML結構嗎?我需要返回一個複雜的XML,如果我把這些標籤放在字符串中,解析器不會將它檢測爲XML響應,它會像String一樣檢測它。
感謝
我想在這裏解決,我在這個論壇在許多次來找我個人無法找到一個方法來達到的效果,很多人也許一直在尋找爲特別的兩大問題直到我找到解決辦法。
1.蝕嚮導生成的網絡服務的節點「返回」下返回該值(包括複雜的對象);問題是很多人都在尋找「如何擺脫這個節點」:)。這個過程有點複雜,但一旦完成,將很容易維護任何格式,希望以他們自己的格式發送他/她的Web服務回覆。
2.如何動態添加處理程序,以實現上述第一個目標。
首先在服務方法:
請添加軟件包org.apache.axis2.context.MessageContext
獲取的配置根上下文 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());
現在添加動態處理程序的哪個軸階段,我們感興趣的是 -
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布爾,你需要使下一次重新設置的某個地方這個處理程序設置代碼不會被調用。
雖然我們將在處理程序中做同樣的事情;如果我們想(在此特別是解決問題的此的書面記錄給出)覆蓋返回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 :)
在同創建一個類包(即com.something.yourpackage您的處理程序方法所在的)和它實現處理程序 -
public class FaltuHandler implements Handler {
......
}
覆蓋一個填寫方法並添加任何你想要的代碼。 我直接從我的項目空間複製了一些部分;添加/刪除的可能是相關的項目
@Override
公共無效清除(){
}
@覆蓋 公共無效flowComplete(MessageContext中爲arg0){
}
@Override public HandlerDescription getHandlerDesc(){
handlerDesc = new HandlerDescription();
handlerDesc.setHandler(this);
handlerDesc.setName(this.getName());
return handlerDesc;
}
@Override 公共字符串的getName(){
return "FaltuHandler";
}
@Override 公共參數的getParameter(字符串爲arg0){
return null;
}
@覆蓋 公共無效的init(HandlerDescription爲arg0){// 沒有必要做任何事情 }
@覆蓋 公共InvocationResponse的invoke(MessageContext的arg0中)拋出AxisFault {
//這是我們需要根據我們的項目特定 //需要做一些討厭的工作的地方。這部分我在這段代碼中內聯解釋。
SOAPEnvelope env = arg0.getEnvelope(); SOAPBody spbd =(SOAPBody)env.getBody(); System.out.println(「從處理程序打印SOAP主體:」+ spbd.toString());
Iterator iterator = spbd.getChildElements();
而(iterator.hasNext()){
的OMElement元素=(的OMElement)iterator.next(); (「從子句處理(頂層子元素) - >」+ 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。 如何從服務 2.如何定製返回值/ XML是擺脫「返回」元素從eclipse嚮導生成的web服務返回 3.如何動態添加處理程序到軸階段
雖然我試圖刪除所有不必要的代碼,我有rel傾向於我的項目;但無法完全刪除。儘管如此,我們仍然可以理解爲了實現我們開始的主要目標,我們需要做些什麼纔是可以理解的。請赦免一些大括號,如果不匹配。
謝謝 Amit