[b]用xfire开发webservice总结[/b]
本实例是以简单的加减法(a+b和a-b)运算实现用xfire开发webservice服务端和客户端。
一、开发环境
运行环境: Tomcat 6.0.14,jdk 1.5
开发工具:Eclipse3.2 + MyEclipse 5.5
二、开发service(服务端)
开发之前请先到官方(http://xfire.codehaus.org/Download)下载xfire-distribution-1.2.6.zip并解压缩.
1.在MyEclipse 建立一个web工程,命名为:XFireWebService,在src目录下新建一个META-INF文件夹里包含xfire/service.xml,把xfire-1.2.6里lib所有jar和xfire-all-1.2.6.jar拷贝到工程lib里,工程具体信息如附件图
2.修改WebRoot/Web-INF/web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<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>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
3.修改src/META-INF/xfire/services.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>MathService</name>
<namespace>
http://localhost:8080/XFireWebService/MathService
</namespace>
<serviceClass>com.test.ws.MathServiceImpl</serviceClass>
</service>
</beans>
4.写MathService接口类和MathServicImpl实现类
MathService接口类
package com.test.ws;
public interface MathService {
public long add(int p1, int p2);
public long minus(int p3, int p4);
}
MathServiceImpl实现类
package com.test.ws;
public class MathServiceImpl implements MathService{
public long add(int p1, int p2) {
System.out.println(p1 + p2);
return p1 + p2;
}
public long minus(int p3, int p4) {
System.out.println(p3 - p4);
return p3 - p4;
}
}
5.MyEclipse会自动编译src文件,请到MyEclipse的workspace里找到刚才建的XFireWebService工程,打开工程并copy WebRoot文件夹到%TOMCAT_HOME%/webapps里并把WebRoot改名为xfire,参照图如附件:
三、开发client(客户端)
开发client有几种模式
1. 纯POJO模式
需要service端提供MathService接口类
package com.test.client;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFire;
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.test.ws.MathService;
public class CallXfireWebService_POJO {
/**
* @param args
*/
public static void main(String[] args) {
Service serviceModel = new ObjectServiceFactory().create(MathService.class);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String serviceUrl = "http://localhost:8080/xfire/services/MathService";
int p1 = 115;
int p2 = 4;
int p3 = 9;
int p4 = 240;
MathService client = null;
try {
client = (MathService) factory.create(serviceModel, serviceUrl);
} catch (MalformedURLException e) {
System.out.println( "Client call webservice has exception: " + e.toString());
}
int result1 = (int) client.add(p1, p2);
int result2 = (int) client.minus(p3, p4);
System.out.println(result1);
System.out.println(result2);
}
}
2. JSR181模式
Server端开发组需要向Client提供MathService的实现类MathServiceImpl,而不止于接口,有点危险,不提倡用这个模式。暂不研究。
3. SpringSide
暂空着,下轮用到再详解。
4. 动态模式
动态模式不需要服务端的class,但在性能上有所欠佳。
package com.test.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
public class DynamicXfireWebService {
/**
* @param args
*/
public static void main(String[] args) {
Client client = null;
try {
client = new Client(new URL("http://localhost:8080/xfire/services/MathService?wsdl"));
Object[] result1 = client.invoke("add", new Object[] {1, 2});
Object[] result2 = client.invoke("minus", new Object[] {1, 2});
System.out.println(result1[0]);
System.out.println(result2[1]);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
5. 根据WSDL生成Client Stub
创建client文件夹里含build.xml文件,内容如下
<?xml version="1.0"?>
<project default="genfiles" basedir=".">
<property name="lib" value="WebRoot/WEB-INF/lib" />
<path id="myclasspath">
<fileset dir="${lib}">
<include name="*.jar" />
</fileset>
<pathelement location="${genfiles}" />
</path>
<property name="code_path" value="src" />
<property name="wsdl_path" value="http://localhost:8080/xfire/MathService?wsdl" />
<property name="code_package" value="com.playphone.ws.client" />
<target name="genfiles" description="Generate the files">
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="myclasspath" />
<wsgen outputDirectory="${code_path}" wsdl="${wsdl_path}" package="${code_package}" binding="xmlbeans" overwrite="true" />
</target>
</project>
用ms dos打开至client文件夹,执行ant.
在所生成的client工程里写一个含math方法的ClientTest类
四、启动Tomcat, 测试
运行客户端程序即可看到结果。
Good luck to you!
:arrow:
本实例是以简单的加减法(a+b和a-b)运算实现用xfire开发webservice服务端和客户端。
一、开发环境
运行环境: Tomcat 6.0.14,jdk 1.5
开发工具:Eclipse3.2 + MyEclipse 5.5
二、开发service(服务端)
开发之前请先到官方(http://xfire.codehaus.org/Download)下载xfire-distribution-1.2.6.zip并解压缩.
1.在MyEclipse 建立一个web工程,命名为:XFireWebService,在src目录下新建一个META-INF文件夹里包含xfire/service.xml,把xfire-1.2.6里lib所有jar和xfire-all-1.2.6.jar拷贝到工程lib里,工程具体信息如附件图
2.修改WebRoot/Web-INF/web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<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>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
3.修改src/META-INF/xfire/services.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>MathService</name>
<namespace>
http://localhost:8080/XFireWebService/MathService
</namespace>
<serviceClass>com.test.ws.MathServiceImpl</serviceClass>
</service>
</beans>
4.写MathService接口类和MathServicImpl实现类
MathService接口类
package com.test.ws;
public interface MathService {
public long add(int p1, int p2);
public long minus(int p3, int p4);
}
MathServiceImpl实现类
package com.test.ws;
public class MathServiceImpl implements MathService{
public long add(int p1, int p2) {
System.out.println(p1 + p2);
return p1 + p2;
}
public long minus(int p3, int p4) {
System.out.println(p3 - p4);
return p3 - p4;
}
}
5.MyEclipse会自动编译src文件,请到MyEclipse的workspace里找到刚才建的XFireWebService工程,打开工程并copy WebRoot文件夹到%TOMCAT_HOME%/webapps里并把WebRoot改名为xfire,参照图如附件:
三、开发client(客户端)
开发client有几种模式
1. 纯POJO模式
需要service端提供MathService接口类
package com.test.client;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFire;
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.test.ws.MathService;
public class CallXfireWebService_POJO {
/**
* @param args
*/
public static void main(String[] args) {
Service serviceModel = new ObjectServiceFactory().create(MathService.class);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String serviceUrl = "http://localhost:8080/xfire/services/MathService";
int p1 = 115;
int p2 = 4;
int p3 = 9;
int p4 = 240;
MathService client = null;
try {
client = (MathService) factory.create(serviceModel, serviceUrl);
} catch (MalformedURLException e) {
System.out.println( "Client call webservice has exception: " + e.toString());
}
int result1 = (int) client.add(p1, p2);
int result2 = (int) client.minus(p3, p4);
System.out.println(result1);
System.out.println(result2);
}
}
2. JSR181模式
Server端开发组需要向Client提供MathService的实现类MathServiceImpl,而不止于接口,有点危险,不提倡用这个模式。暂不研究。
3. SpringSide
暂空着,下轮用到再详解。
4. 动态模式
动态模式不需要服务端的class,但在性能上有所欠佳。
package com.test.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
public class DynamicXfireWebService {
/**
* @param args
*/
public static void main(String[] args) {
Client client = null;
try {
client = new Client(new URL("http://localhost:8080/xfire/services/MathService?wsdl"));
Object[] result1 = client.invoke("add", new Object[] {1, 2});
Object[] result2 = client.invoke("minus", new Object[] {1, 2});
System.out.println(result1[0]);
System.out.println(result2[1]);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
5. 根据WSDL生成Client Stub
创建client文件夹里含build.xml文件,内容如下
<?xml version="1.0"?>
<project default="genfiles" basedir=".">
<property name="lib" value="WebRoot/WEB-INF/lib" />
<path id="myclasspath">
<fileset dir="${lib}">
<include name="*.jar" />
</fileset>
<pathelement location="${genfiles}" />
</path>
<property name="code_path" value="src" />
<property name="wsdl_path" value="http://localhost:8080/xfire/MathService?wsdl" />
<property name="code_package" value="com.playphone.ws.client" />
<target name="genfiles" description="Generate the files">
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="myclasspath" />
<wsgen outputDirectory="${code_path}" wsdl="${wsdl_path}" package="${code_package}" binding="xmlbeans" overwrite="true" />
</target>
</project>
用ms dos打开至client文件夹,执行ant.
在所生成的client工程里写一个含math方法的ClientTest类
四、启动Tomcat, 测试
运行客户端程序即可看到结果。
Good luck to you!
:arrow: