myeclipse下用XFire开发webservice
1.new->other->MyEcplise->Web Services->Web Services
自动在classpath: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.semovy.service.IUserService</serviceClass>
<implementationClass>
com.semovy.service.impl.UserServiceImpl
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
2.在WEB-INF下的web.xml下添加servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
3.com.semovy.beans下建立测试类User
User.java
package com.semovy.beans;
public class User {
private String id = "";
private String name = "";
private String remark = "";
public User() {
}
public User(String id, String name, String remark) {
this.id = id;
this.name = name;
this.remark = remark;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String toString() {
return "Id: " + id + " , name: " + name + " , remark: " + remark + "\n";
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((id == null) ? 0 : id.hashCode());
result = PRIME * result + ((name == null) ? 0 : name.hashCode());
result = PRIME * result + ((remark == null) ? 0 : remark.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (remark == null) {
if (other.remark != null)
return false;
} else if (!remark.equals(other.remark))
return false;
return true;
}
}
在com.semovy.service包下建立服务接口IUserService
IUserService.java
package com.semovy.service;
import java.util.List;
import com.semovy.beans.User;
public interface IUserService {
public abstract String getStringTest(String msg1,String msg2);
public abstract List getStringList();
public abstract List getUsersList();
public abstract User[] getUsersArray();
public abstract String[] getUsersStringArray();
public abstract User getUserById(String id);
public abstract void addUser(User user);
}
这在接口包下建立.与服务接口相同名称的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.semovy.beans.User"/>
</method>
<method name="getUsersArray">
<return-type componentType="com.semovy.beans.User"/>
</method>
<method name="getUserById">
<return-type componentType="com.semovy.beans.User"/>
</method>
</mapping>
</mappings>
在com.semovy.service.impl包下
UserServiceImpl.java
package com.semovy.service.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.semovy.beans.User;
import com.semovy.service.IUserService;
/**
*
* @author semovy
* @version 1.0
* the user service class implemente the IUserService implementation for testing
*/
public class UserServiceImpl implements IUserService {
private List<User> users;
public UserServiceImpl()
{
users = new ArrayList<User>();
users.add(new User("1","semovy","semovy's remark"));
users.add(new User("2","韦善茂","韦善茂的备注"));
users.add(new User("3","superman_wshm","superman_wshm's remark"));
}
/**
* @param msg1 the string msg1
* @param msg2 the string msg2
*
*/
public String getStringTest(String msg1,String msg2) {
return "Hello :" + msg1 + " : " + msg2;
}
/**
* @return list the list Collection with some String element
*/
public List getStringList()
{
List<String> list = new ArrayList<String>();
list.add("string1");
list.add("字符串2");
list.add("字符串3");
list.add("string4");
return list;
}
/**
* @return all users
*/
public List getUsersList() {
return users;
}
/**
* @param id the id of user
* @return the user by specifical id
*/
public User getUserById(String id) {
Iterator it = users.iterator();
while(it.hasNext())
{
User user = (User)it.next();
if(user.getId().equals(id))
{
return user;
}
}
return null;
}
/**
* @return return all users of array form with type User.
*/
public User[] getUsersArray() {
return users.toArray(new User[]{new User()});
}
/**
* @return return all users of array form with type String
*/
public String[] getUsersStringArray() {
User[] users_arr = (User[])users.toArray(new User[]{});
String[] tempUser_arr = new String[users_arr.length];
for(int i=0;i<users_arr.length;i++)
{
tempUser_arr[i] = users_arr[i].toString();
System.out.println(tempUser_arr[i]);
}
return tempUser_arr;
}
public void addUser(User user) {
users.add(user);
}
}
4.在com.semovy.test包下
建立客户调用测试类.UserServiceWSTest .java
package com.semovy.test;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.semovy.beans.User;
import com.semovy.service.IUserService;
public class UserServiceWSTest {
public static void main(String[] args) {
// 创建服务实例
Service srvcModel = new ObjectServiceFactory()
.create(IUserService.class);
XFireProxyFactory factory = // 创建代理工厂实例
new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String helloWorldURL = "http://localhost:8000/webServiceTest/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 strList = srvc.getStringList();
Iterator str_it = strList.iterator();
while (str_it.hasNext()) {
String str = (String) str_it.next();
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 userList = srvc.getUsersList();
Iterator userList_it = userList.iterator();
while (userList_it.hasNext()) {
User user = (User) userList_it.next();
System.out.print(user.toString());
}
}
}
5.结果:
Hello :msg1 : msg2
Id: 1 , name: semovy , remark: semovy's remark
Id: 2 , name: 韦善茂 , remark: 韦善茂的备注
Id: 3 , name: superman_wshm , remark: superman_wshm's remark
string1
字符串2
字符串3
string4
Id: 1 , name: semovy , remark: semovy's remark
Id: 2 , name: 韦善茂 , remark: 韦善茂的备注
Id: 3 , name: superman_wshm , remark: superman_wshm's remark
Id: 2 , name: 韦善茂 , remark: 韦善茂的备注
Id: 1 , name: semovy , remark: semovy's remark
Id: 2 , name: 韦善茂 , remark: 韦善茂的备注
Id: 3 , name: superman_wshm , remark: superman_wshm's remark
Id: 4 , name: 添加的名称 , remark: 添加的备注