1. 首先,先编写服务端代码
public class ReturnObjectService {
public User getUser(String name) {
User user = new User();
user.setUsername(name);
return user;
}
}
package server.object;
import java.io.Serializable;
public class User implements Serializable{
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
2. 进行手动打包过程。 新建一个文件夹,放入对应的服务端class(要求带入文件路径)。新建一个META-INF文件夹,并在其中编写services.xml文件,具体内容如下:
<service name="returnObjectService">
<parameter name="ServiceClass">
server.object.ReturnObjectService
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</messageReceivers>
</service>
可以用开始进行打包处理,打包语句如下:进入需要打包的文件路径,直接执行 jar cvf 打包后名字.aar . (后面有个点,表示当前路径),
或者jar cvf 打包后名字.aar 文件夹名 文件夹名 等两种方式都可以。
打包后,把对应的aar包放入Tomcat6.0\webapps\axis2\WEB-INF\services 路径之下,就可以了。
3. 编写客户端代码:
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.rpc.client.RPCServiceClient;
public class ReturnObjectTest {
public static void main(String[] args) throws AxisFault {
String target = "http://localhost:8080/axis2/services/returnObjectService";
RPCServiceClient client = new RPCServiceClient();
Options options = client.getOptions();
options.setManageSession(true);
EndpointReference epr = new EndpointReference(target);
options.setTo(epr);
QName qname = new QName("http://object.server", "getUser");
Object[] result = client.invokeBlocking(qname, new Object[] {"linwei"},new Class[] { User.class });
User user = (User)result[0];
System.out.println("user.username is " + user.getUsername());
}
}