XFire使用详解
最近一直使用XFire做WebServices方式使用,总结一些相关经验,本文以具体例子进行XFire开发过程分析
1、首先定义一个Interface,具体包含简单类型传值、返回自定义对象、返回集合三个接口方法
import java.util.List;
import com.gresoft.commons.core.exception.BusinessException;
import com.gresoft.sanitation.model.dictionary.Hospital;
public interface WebServicesInterface {
public String savePutXml(int i,String typecode) throws BusinessException;
public Hospital getList(int p);
public List getAll();
}2、实现上述接口,实现业务逻辑
public class HospitalService extends HibernateEntityDao<Hospital> implements
WebServicesInterface {
private HospitalManager hospitalManager;
public void setHospitalManager(HospitalManager hospitalManager) {
this.hospitalManager = hospitalManager;
}
public String savePutXml(int o, String typecode) throws BusinessException {
// TODO Auto-generated method stub
Hospital h = new Hospital(o, typecode);
super.save(h);
System.out.println("ccccc");
return "gggg";
}
public Hospital getList(int p) {
// TODO Auto-generated method stub
Hospital h = hospitalManager.get(p);
System.out.println("name:" + h.getHospitalName());
return hospitalManager.get(p);
}
@SuppressWarnings("unchecked")
public List getAll() {
List list = hospitalManager.getAll();
System.out.println("size:" + list.size());
return hospitalManager.getAll();
}
}定义自定义对象Hospital
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
/**
* @author wuxj:
* @version 创建时间:2007-10-15 上午11:31:39
*/
@Entity
@Table(name = "HOSPITAL", schema = "DB2ADMIN")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Hospital implements java.io.Serializable {
private int hospitalId;
private String hospitalName;
public Hospital() {
}
public Hospital(int hospitalId) {
this.hospitalId = hospitalId;
}
public Hospital(int hospitalId, String hospitalName) {
this.hospitalId = hospitalId;
this.hospitalName = hospitalName;
}
@Id
@Column(name = "HOSPITAL_ID", unique = true, nullable = false)
public int getHospitalId() {
return hospitalId;
}
public void setHospitalId(int hospitalId) {
this.hospitalId = hospitalId;
}
@Column(name = "HOSPITAL_NAME", nullable = false, length = 20)
public String getHospitalName() {
return hospitalName;
}
public void setHospitalName(String hospitalName) {
this.hospitalName = hospitalName;
}
}3、编写XFire的描述文件
描述文件为WebServicesInterface.aegis.xml,一定要与Interface接口类在同一个目录,否则会出错误
只需要描述返回是集合的描述,getAll方法返回是List集合,需要对集合中的元素进行定义,在这里是Hospital对象
<?xml version="1.0" encoding="UTF-8"?> <mappings> <mapping> <method name="getAll"> <return-type componentType="com.gresoft.sanitation.model.dictionary.Hospital" /> </method> <property name="hibernateGenericDao" ignore="true" /> <!-- 在HospitalService中这个文件可以不用,主要是为了定义返回为对象或集合 --> <!-- <method name="savePutXml"> <parameter index="1" componentType="java.lang.String" /> </method> --> </mapping> </mappings>
4、编写xfire-servlet.xml文件
xfire是跟spring结合一起使用的,所以用到了spring相关内容。
注意:位置一定要在WEB-INF目录下
key="/echo"中的echo为你在客户端访问的地址,具体看client内容
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- START SNIPPET: xfire --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/echo" value-ref="echo" /> </map> </property> </bean> <!-- Declare a parent bean with all properties common to both services --> <bean id="echo" class="org.codehaus.xfire.spring.remoting.XFireExporter"> <property name="serviceFactory" ref="xfire.serviceFactory" /> <property name="xfire" ref="xfire" /> <property name="serviceBean" ref="hospitalService" /> <property name="serviceInterface" value="com.gresoft.sanitation.service.WebServicesInterface" /> </bean> <!-- END SNIPPET: xfire --> </beans>
5、编写客户端测试
import java.net.MalformedURLException;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class HospitalServiceClient {
public static void main(String args[]) {
Service srvcModel = new ObjectServiceFactory()
.create(WebServicesInterface.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
// XFireProxyFactory factory = new XFireProxyFactory(getXFire());
String helloWorldURL = "http://localhost:8080/sanitation/service/echo";
// String helloWorldURL ="xfire.local://echo";
try {
WebServicesInterface srvc = (WebServicesInterface) factory.create(
srvcModel, helloWorldURL);
Hospital h = new Hospital();
h.setHospitalId(11);
h.setHospitalName("测试");
// System.out.print(srvc.sayHello("wuxj")+"\n");
// RegisterApply apply = new RegisterApply();
// apply.setOperateDate("20071221");
// apply.setRegisterId("1000");
// srvc.savePutXml(apply);
System.out.println(srvc.savePutXml(11, "测试"));
srvc.getList(1);
srvc.getAll();
// System.out.print(((MsdpUser)srvc.getUser().get(0)).getUid());
} catch (MalformedURLException e) {
System.out.println("wrong");
e.printStackTrace();
}
}
}6、定义web.xml的xfire
红色字体一定要加上,而且是固定格式,这是xfire需要加载的文件
<?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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/*.xml,classpath:org/codehaus/xfire/spring/xfire.xml</param-value> </context-param> <!--Spring ApplicationContext 载入 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- XFire WebServices的入口 --> <servlet> <servlet-name>xfire</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xfire</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping> </web-app>