webservice之xfire服务搭建及客户端请求(maven搭建项目)
- 运行环境
(1) 操作系统:windows10
(2) JDK : 1.7
(3) IDE : Myeclipse10
(4) xfire : xfire1.2.6
(5) 服务器:tomcat7
- 服务端环境搭建步骤
(1) 配置maven的配置文件,下载xfire
(2) 配置web.xml
<?xml version=”1.0” encoding=”UTF-8”?>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
(3) 创建接口
在src/main/java目录下,创建自定义目录(如:com.test.xfire),在自定义目录下创建接口:IHelloService 及实现类 HelloServiceImpl
A. IHelloService.java
package com.test.service.xfire;
public interface IHelloService {
void hello(String name);
}
B. HelloServiceImpl.java
package com.test.service.xfire.impl;
import com.test.service.xfire.IHelloService;
public class HelloServiceImpl implements IHelloService {
@Override
public void hello(String name) {
System.out.println(“—-xfire—hello —“ + name);
}
}
(4) 配置xfire 接口文件services.xml
在WEB-INF目录下创建文件 \META-INF\xfire\services.xml ,完整路径如:src\main\webapp\WEB-INF\META-INF\xfire\services.xml
注: \META-INF\xfire\services.xml 中的xfire为自定义文件夹名称
services.xml的配置方式如下:
<?xml version=”1.0” encoding=”UTF-8”?>
(5) 将项目发布到tomcat服务器,在浏览器地址栏输入:http://192.168.1.133:8080/TestXfireService/services/HelloService?wsdl ,显示接口信息
在同一应用中创建xfire客户端进行调用
package com.test.client;
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;
import com.test.service.xfire.IHelloService;
public class TestClientMain {
/**
* @param args
* @throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException {
Service service = new ObjectServiceFactory().create(IHelloService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
String url = “http://192.168.1.133:8080/TestXfireService/services/HelloService“;
IHelloService helloService = (IHelloService) factory.create(service,url);
helloService.hello(“张三”);
}
}
注:客户端调用时,url路径中不能 带 “?wsdl”
还没有评论,来说两句吧...