service.IUserService.java
public interface IUserService{
String getStringTest(String msg1, String msg2);
List<String> getStringList();
List<User> getUsersList();
User[] getUsersArray();
String[] getUsersStringArray();
User getUserById(String id);
void addUser(User user);
}
service.IUserService.aegis.xml
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping><!-- 返回类型是String ,String[]的,参数是String类型的方法不用定义 -->
<method name="getStringList">
<return-type componentType="java.lang.String" />
<!--返回类型对应集合Collection,数组中的元素的类型-->
</method>
<method name="getUsersList">
<return-type componentType="com.yongjie.ws.xfire.beans.User" />
</method>
<method name="getUsersArray">
<return-type componentType="com.yongjie.ws.xfire.beans.User" />
</method>
<method name="getUserById">
<return-type componentType="com.yongjie.ws.xfire.beans.User" />
</method>
</mapping>
</mappings>
META-INF/xfire/services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>UserService</name>
<serviceClass>com.yongjie.ws.xfire.service.IUserService</serviceClass>
<implementationClass>com.yongjie.ws.xfire.service.impl.UserServiceImpl</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
client.TestUserService.java
public class TestUserService{
public static void main(String[] args) {
// 创建服务实例
Service srvcModel = new ObjectServiceFactory().create(IUserService.class);
// 创建代理工厂实例
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String helloWorldURL = "http://localhost:8081/WSXFire/services/UserService";
IUserService srvc = null;
try{ // 获取指定位置的web服务对象
srvc = (IUserService)factory.create(srvcModel,helloWorldURL);
}catch(MalformedURLException e){
e.printStackTrace();
}
//调用只返回String的方法.在接口名.aegis.xml中不用设置方法名
String result = srvc.getStringTest("msg1","msg2");
System.out.println(result);
//调用返回String[]数组的方法,在接口名.aegis.xml中不用设置方法名.
String[] userInfo_str_arr = srvc.getUsersStringArray();
for(String aUserInfo : userInfo_str_arr){
System.out.println(aUserInfo);
}
//调用返回List类型值的方法,需要定义,List容器中的元素类型为String
List<String> strList = srvc.getStringList();
for(String str : strList){
System.out.println(str);
}
//调用返回User[]数组
User[] user_arr = srvc.getUsersArray();
for(User a_user : user_arr){
System.out.print(a_user.toString());
}
//调用返回User类型值的方法.方法参数是字符串.
User a_user = srvc.getUserById("2");
System.out.print(a_user.toString());
//调用参数为User的方法,没有返回值,不用配置方法
srvc.addUser(new User("4","添加的名称","添加的备注"));
//调用返回List类型值的方法,需要定义,List容器中的元素类型为User
List<User> userList = srvc.getUsersList();
for(User user : userList){
System.out.print(user.toString());
}
}
}