posts - 0, comments - 77, trackbacks - 0, articles - 356
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

myeclipse下用XFire开发webservice

Posted on 2008-03-08 17:07 semovy 阅读(5683) 评论(8)  编辑  收藏 所属分类: WebService
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: 添加的备注


评论

# re: myeclipse下用XFire开发webservice[未登录]  回复  更多评论   

2008-04-07 13:20 by dd
测试不是远程调,如果是远程的怎么Service srvcModel = new ObjectServiceFactory()
.create(IUserService.class);
如果调用的工程没有IUserService.class怎么办?

# re: myeclipse下用XFire开发webservice  回复  更多评论   

2008-06-04 14:58 by zhouyu
我先建User类,然后用myeclipse自动生成客户端时,就会把User类里我自已写
的代码全给换了,参数全是JAXBElement<String>类型的,于是我在客户端传入
JAXBElement<String>类型的参数,结果在服务器那边用user.getName()取值为空,user.getName().getValue()也没值,但user这个对像是不为空的,这是为什么呢????
我邮箱maosha8@163.com
谢谢了帮我解决下吧

# re: myeclipse下用XFire开发webservice[未登录]  回复  更多评论   

2010-01-27 21:10 by ccc
非常感谢!!终于可以调用Webservice返回List,不为空了,IUserService.aegis.xml这个文件至关重要

# re: myeclipse下用XFire开发webservice  回复  更多评论   

2010-05-13 01:37 by xc
您好,我也碰到和楼上一样的问题,myeclipse自动生成客户端时把自定义类里的类型都变成JAXBElement类型,我客户端传入JAXBElement类型,可在服务器上user.getName()为空,user.getName().getValue()就报错,2010-5-12 19:16:52 org.codehaus.xfire.handler.DefaultFaultHandler invoke
严重: Fault occurred!
能不能再帮忙回答一下!非常感谢!
zephyr.xu@gmail.com

# re: myeclipse下用XFire开发webservice  回复  更多评论   

2011-07-29 10:24 by 剑天
@dd
如果在别的地方掉,那么需要创建一个与这个借口一样的接口,才能调用,明白么?

# re: myeclipse下用XFire开发webservice  回复  更多评论   

2011-12-19 07:16 by 游客
@ccc
怎么弄的啊?我的一直为空

# re: myeclipse下用XFire开发webservice  回复  更多评论   

2011-12-19 07:32 by 游客
@zhouyu
你好,这个问题你解决了吗?已经困扰我一晚上了
如果解决请帮忙回答一下
makeboluo000@163.com
谢谢

# re: myeclipse下用XFire开发webservice  回复  更多评论   

2011-12-19 15:57 by 游客
你是用远程还是在本地同一个工程文件中?@ccc

只有注册用户登录后才能发表评论。


网站导航: