服务端:
接口:
package com.xfire.test;
import java.io.Serializable;
import java.util.List;
import com.xfire.VO.User;
public interface MathNumber extends Serializable{
public long add(int a,int b);
public List<User> getlist();
public String[] getStrins();
}
实现接口类:
package com.xfire.test.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.sun.org.apache.xml.internal.utils.SerializableLocatorImpl;
import com.xfire.VO.User;
public class MathNumber extends SerializableLocatorImpl implements Serializable,com.xfire.test.MathNumber {
public long add(int a, int b) {
return a+b;
}
public List<User> getlist() {
List<User> list=new ArrayList<User>();
for(int i=0;i<10;i++){
User user=new User("1","luojiawen","luojiawen","china","13632581936");
list.add(user);
}
return list;
}
public String[] getStrins() {
String[] string ={"abc","abcd","abcde","abcdef","abcdefg"};
return string;
}
}
services.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>weather</name>
<namespace>http://com.xfire.test/Test</namespace>
<serviceClass>com.xfire.test.weather</serviceClass>
<implementationClass>com.xfire.test.impl.weatherImpl</implementationClass>
</service>
<service>
<name>Math</name>
<namespace>http://com.xfire.test/MathNumber</namespace>
<serviceClass>com.xfire.test.MathNumber</serviceClass>
<implementationClass>com.xfire.test.impl.MathNumber</implementationClass>
</service>
</beans>
web.xml配置:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- START SNIPPET: webxml -->
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>XFireServlet</servlet-name>
<display-name>XFire Servlet</display-name>
<servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
<!-- END SNIPPET: webxml -->
客户端:
在客户端要写用到的接口:
package com.xfire.test;
import java.io.Serializable;
import java.util.List;
import com.xfire.VO.User;
public interface MathNumber extends Serializable {
public long add(int a,int b);
public List<User> getlist();
public String[] getStrins();
}
客户端的实现,请求服务得到相应:
package com.xfire.client;
import java.net.MalformedURLException;
import java.util.List;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.xfire.VO.User;
import com.xfire.test.MathNumber;
import com.xfire.test.weather;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/* try {
String serviceURL="http://localhost:8090/myXfires/services/weather";
Service serviceModel = new ObjectServiceFactory().create(weather.class);
XFireProxyFactory serviceFactory = new XFireProxyFactory();
weather service = (weather) serviceFactory.create(serviceModel, serviceURL);
service.weather();
} catch (Exception e) {
e.printStackTrace();
}*/
try {
String serviceURL="http://localhost:8090/myXfires/services/Math";
Service serviceModel = new ObjectServiceFactory().create(MathNumber.class);
XFireProxyFactory serviceFactory = new XFireProxyFactory();
MathNumber service = (MathNumber) serviceFactory.create(serviceModel, serviceURL);
System.out.println(service.add(1, 2));
List<User> list= service.getlist();
System.out.println(list.size());
for (User user : list) {
System.out.println(user.getUser_id()+"-----"+user.getUser_name()+"-----"+user.getUser_password());
}
System.out.println(service.getStrins().length);
} catch (Exception e) {
e.printStackTrace();
}
}
}