1.使用Axis2的底層API開發Web Service Server端
1.1建立一個WebService(取名為MyService)
在MyService中有兩個operations,如下所示。
public void ping(OMElement element){}//IN-ONLY模式。僅僅接收OMElement,並對 其處理。
public OMElement echo(OMElement element){}//IN_OUT模式。接收OMElemen,並返回 OMElement。
1.2如何寫Web Service
1)建立實現服務的類。
2)建立services.xml來解析這個Web Service。
3)將其打包成一個*.aar文檔(Axis Archive)。
4)部署Web Service。
1.2.1 建立實現服務的類
此類中提供的方法必須與Web Service(在services.xml中聲明)中的operations對應 。除非你提供了資料繫結,否則所有的方法只能接收一個參數,其類型為OMElement。
public class MyService{
public void ping(OMElement element){...}
public OMElement echo(OMElement element){...}
}
MyService.java
package userguide.example1;
import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import javax.xml.stream.XMLStreamException;
public class MyService {
public OMElement echo(OMElement element) throws XMLStreamException {
//Praparing the OMElement so that it can be attached to another OM Tree.
//First the OMElement should be completely build in case it is not fully built and still
//some of the xml is in the stream.
element.build();
//Secondly the OMElement should be detached from the current OMTree so that it can
// be attached some other OM Tree. Once detached the OmTree will remove its
// connections to this OMElement.
element.detach();
return element;
}
public void ping(OMElement element) throws XMLStreamException {
//Do some processing
}
public void pingF(OMElement element) throws AxisFault{
throw new AxisFault("Fault being thrown");
}
}