サンプル
使用するWebサービス
* Axis2で自作したサービス(例えば、以下のURLを参考)を起動させて、実行したhttp://blogs.yahoo.co.jp/dk521123/31944955.html
RestClientWithAxis2.java
* Axis2 についてるサンプル「axis2-1.X.X\samples\yahoorestsearch\src\sample\yahooservices\RESTSearch」を参考に作成import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import java.io.UnsupportedEncodingException; import java.util.Iterator; public class RestClientWithAxis2 { private static final String Url = "http://localhost:8080/SampleServerWithAxis2/services/HelloWorld/SayHello?name=mike"; public static void main(String[] args) throws org.apache.axis2.AxisFault, UnsupportedEncodingException { ServiceClient client = new ServiceClient(); Options options = new Options(); client.setOptions(options); options.setTo(new EndpointReference(RestClientWithAxis2.Url)); options.setProperty( Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE); options.setProperty( Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_GET); OMElement response = client.sendReceive( RestClientWithAxis2.createOMElement()); RestClientWithAxis2.display(response); } private static OMElement createOMElement() throws UnsupportedEncodingException { OMFactory fac = OMAbstractFactory.getOMFactory(); OMElement rootElement = fac.createOMElement("webSearch", null); return rootElement; } private static void display(OMElement response) { //get an iterator for Result elements Iterator itr = response.getChildElements(); while (itr.hasNext()) { OMElement result = (OMElement) itr.next(); Iterator innerItr = result.getChildElements(); System.out.println("result = " + result); System.out.println("innerItr = " + innerItr); } } }
出力
result = <ns:return xmlns:ns="Hello">http://services.demo">Hello mike</ns:return> innerItr = org.apache.axiom.om.impl.llom.OMChildElementIterator@1e7ed2d
参考資料
http://dodosko-it.jugem.jp/?eid=31http://axis2.exblog.jp/1649903
http://szk-takanori.hatenablog.com/entry/20060215/1140016085
http://axis.apache.org/axis2/java/core/docs/rest-ws.html
http://ameblo.jp/aac-sandi/entry-10035620587.html