axis2入门实例
Server:(part)
package service;
import java.util.Arrays;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.context.ServiceGroupContext;
import domain.Data;
public class Login {
public String getContainName(String[] names) {
return Arrays.toString(names);
}
public String[] getNames() {
return new String[] { "Hello", "Hi" };
}
public Data getData(Data data, String name) {
data.setDscr("Welcome: " + name);
return data;
}
public void getLogin(String key, String value) {
MessageContext mc = MessageContext.getCurrentMessageContext();
ServiceContext sc = mc.getServiceContext();
sc.setProperty(key, value);
}
public String getLoginName(String key) {
MessageContext mc = MessageContext.getCurrentMessageContext();
ServiceContext sc = mc.getServiceContext();
return (String) sc.getProperty(key);
}
public void getLogout(String key, String value) {
MessageContext mc = MessageContext.getCurrentMessageContext();
ServiceGroupContext sgc = mc.getServiceGroupContext();
sgc.setProperty(key, value);
}
}
package service;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.ServiceGroupContext;
public class LoginService {
public String getLoginName(String key){
MessageContext mc = MessageContext.getCurrentMessageContext();
ServiceGroupContext sgc = mc.getServiceGroupContext();
return (String)sgc.getProperty(key);
}
}
Client:(part)
package client;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class ServiceClient {
public static void main(String[] args) {
try {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
options.setTo(new EndpointReference(" http://127.0.0.1:8080/axis2/services/testContext1"));
options.setManageSession(true);
// 指定要调用的getNames方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://service","getNames");
// 调用getNames方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[] {}, new Class[] { String[].class })[0]);
serviceClient.invokeRobust(opAddEntry, new Object[] {});
serviceClient.invokeNonBlocking(opAddEntry, null, new org.apache.axis2.client.async.AxisCallback() {
@Override
public void onComplete() {
}
@Override
public void onError(Exception arg0) {
}
@Override
public void onFault(MessageContext arg0) {
}
@Override
public void onMessage(MessageContext arg0) {
}
});
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
备注: 没有axis2的相关jar包和基本war包。(推荐到官网上下载)
axis2官网: http://axis.apache.org/axis2/java/core/download.cgi