axis 2 webservice分散開発を実現


詳細
仕事の関係でこの2日間axis 2を勉強してwebserviceを実現して、それからネット上で関連する資料を探して、しかしネット上で私の欲しいものが探し当てていないで、あれらの資料の大部分は書くのがとても乱れて初心者に対して理解しにくいので、私の書いたコードを貼って、後で使うことができる上の友达はいっしょに研究することができます
Webserviceはwebサービスであり、分散開発を実現できる
ここではクライアントとサーバ側のコードを簡単に紹介します(クライアントとサービス側は1つのプロジェクトに置くことができて、2つのプロジェクトに分けることができます)、必要なjarファイルは何があるか分からないならばすべてのaxis 2のjarファイルを試験します
1クライアント1ファイル
import javax.xml.stream.FactoryConfigurationError;

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 org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class Test {

public static void main(String[] args) {
ServiceClient sender = null;
try {
String uri = "http://localhost:8080/Axis2Server/services/CRMMgrService";
String serverName = "ServerResponse";
// String key = "id";
// String num = "001";
//    OMFactory      OMElement  
OMFactory fac = OMAbstractFactory.getOMFactory();
OMElement method = fac.createOMElement(serverName,
OMAbstractFactory.getOMFactory().createOMNamespace(
"http://mcerp.service.pojo/xsd",
"ServerObjectRequest"));// 
// OMElement value = fac.createOMElement(key, null);// 
// value.addChild(fac.createOMText(value, num));
// method.addChild(value);

Options options = new Options();
EndpointReference epr = new EndpointReference(uri);//       
options.setTo(epr);
options.setAction("urn:" + serverName);//     

options.setTransportInProtocol(Constants.TRANSPORT_HTTP);//      HTTP
options.setProperty(Constants.Configuration.ENABLE_REST,
Constants.VALUE_TRUE);//    rest

sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(method);
System.out.println(result.getFirstElement().getText());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sender != null) {
try {
sender.cleanupTransport();
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
}

}

  2.サービス側にはjavaファイルとservicesが必要です.xml
package com.test.service;
/*
 *   version 1.0
 *           ,       
 *       ,    ,    ,             
 */



public class CRMMgrService {

	public void ServerObjectRequest(String str)
	{
		System.out.println("hello world!");
	}
	
	public String ServerResponse()
	{
		return "hello-response";
	}
	
	public String ServerPOJOResponse()
	{
		return "hello-pojo";
	}
}

 
3.サービス側のWEB-INFでservicesCRMMgrMETA-INFファイルディレクトリを新規作成し、META-INFディレクトリでservicesを新規作成する.xml
 


	the pojo web service interfaces
	
		
		
	
	
	
		com.test.service.CRMMgrService
	

これによりクライアントコードを実行するだけでサービス側の応答が得られる