简单搭建xfire开源软件的webservice开发及其步骤:
1、创建好一个web工程,引入xfire下的jar包,注意lib下的和xfire-all.jar
2、定义接口;
package com.iteye.webservice;
public interface IHelloService {
public String sayHelleWorld(String inputStr);
}
3、接口实现;
package com.iteye.webservice;
public class HelloImpl implements IHelloService {
@Override
public String sayHelleWorld(String name) {
System.out.println("hello worl! this is first webservice!!!");
return "Hello "+name;
}
}
4、web.xml配置访问公开接口信息;
以下配置都是固定的配置信息:
<servlet>
<servlet-name>XFireServlet</servlet-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>
5、新建META-INF,新建xfire,新建services.xml,并配置接口信息;
结构:/src/META-INF/xfire/services.xml
servics.xml内容:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>HelloService</name>
<namespace>http://com/iteye/webservice/HelloService</namespace>
<serviceClass>com.iteye.webservice.IHelloService</serviceClass>
<implementationClass>com.iteye.webservice.HelloImpl</implementationClass>
</service>
</beans>
6、部署访问,生成 ?wsdl文件。
加载到tomcat中,启动浏览器地址栏输入:
http://localhost:8080/webservicedemo/services
返回?wsdl文件。
7、模拟远程调用服务器端
创建一个客户端项目(Java工程或者web工程),引入jar包,创建客户端代码:
1、client客户端
@Test
public void testSayHelle() {
try {
Client client = new Client(new URL(
"http://localhost:8090/webservicedemo/services/HelloService?wsdl"));
Object[] objects = client.invoke("sayHelleWorld",
new Object[] { "tom" });
System.out.println(objects[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
2、xfire代理客户端
@Test
public void testFactoryClient() throws MalformedURLException {
// 创建服务工厂
ObjectServiceFactory serviceFactory = new ObjectServiceFactory();
// 通过服务工厂创建服务
Service service = serviceFactory.create(IHelloService.class);
// 创建代理工厂
XFireProxyFactory fireProxyFactory = new XFireProxyFactory();
// 通过代理工厂创建代理对象
IHelloService hs = (IHelloService) ireProxyFactory.create(service,
"http://localhost:8080/webservicedemo/services/HelloService");
// 调用代理对象方法(即远程调用)
hs.sayHelleWorld("tom");
}