使用XFire发布和调用web服务

XFire 是 codeHaus 组织提供个开源框架它构建了 POJO 和 SOA 的间桥梁主要特性就是支持将 POJO 通过非常简单方式发布成 Web 服务这种处理方式不仅充分发挥了 POJO 作用简化了 Java 应用转化为 Web 服务步骤和过程也直接降低了 SOA 实现难度为企业转向 SOA 架构提供了种简单可行方式  

 

 XFire 目前最新版本是 1.2.6目前支持特性主要包括:  

           支持将 Web 服务绑定到 POJO、XMLBeans、JAXB1.1、JAXB2.0 和 Castor; 

           支持基于 HTTP、JMS、XMPP 等多种协议访问 Web 服务; 

           支持多种 Web 服务业界重要标准如 SOAP、WSDL、Web 服务寻址(WS-Addressing)、Web 服务安全(WS-Security)等; 

           支持 JSR181可以通过 JDK5 配置 Web 服务; 

 

 高性能 SOAP 实现;  服务器端、客户端代码辅助生成;  对 Spring、Pico、Plexus 等项目支持等  XFire 安装包   XFire 框架目前最新版本是 1.2.6可以访问 xfire.codehaus.org 下载 XFire 框架安装包下载时请选择“全部 2进制发布包(Binary Distribution in zip package)”而不仅仅是“XFire jar 文件(Jar of all XFire modules)”   下载完成后我们可以将下载 .zip 文件解压缩到任意文件夹中(后面章节中使用 % XFIRE_HOME % 表示 XFire框架安装目录)解压缩后形成文件目录结构如下:   api(目录)  api 目录中是 XFire 框架中所有类()对应 API 文档为开发者使用 XFire 完成应用开发提供帮助   examples(目录)  examples 目录中包含了所有随 XFire  2进制包发布例子包括这些例子源代码和相关 Web 应用配置内容   lib(目录)  lib 目录中包含 XFire 运行所需要外部支持类包(.jar文件)可以根据区别项目所需 XFire 特性选择所需要支持类包保守思路方法是在 Web 项目中包含所有外部支持类包(.jar文件) 

manual 目录中包含有 XFire 框架帮助文档开发者可以从这些帮助文档中学习更多运用 XFire 框架实现 SOA 知识和窍门技巧   modules(目录)  modules 目录中包含了 XFire 框架根据区别特性分别编译 2进制包文件发布基于 XFire 框架 Web 项目时可以选择使用该目录下所有 .jar 文件也可以选择 XFire-all-1.2.6.jar 文件   XFire-all-1.2.6.jar  XFire 框架 2进制包文件包含了全部模块(modules)   LICENSE.txt  LICENSE.txt 文件中包含了 XFire 框架授权协议   NOTICE.txt  README.txt  这两个文件中包含了 XFire 发布时些有用信息

manual 目录中包含有 XFire 框架帮助文档开发者可以从这些帮助文档中学习更多运用 XFire 框架实现 SOA 知识和窍门技巧   modules(目录)  modules 目录中包含了 XFire 框架根据区别特性分别编译 2进制包文件发布基于 XFire 框架 Web 项目时可以选择使用该目录下所有 .jar 文件也可以选择 XFire-all-1.2.6.jar 文件   XFire-all-1.2.6.jar  XFire 框架 2进制包文件包含了全部模块(modules)   LICENSE.txt  LICENSE.txt 文件中包含了 XFire 框架授权协议   NOTICE.txt  README.txt  这两个文件中包含了 XFire 发布时些有用信息

创建接口:

public interface SumService {
	public int sum(int n);
}

创建实现类:

public class SumServiceImpl implements SumService {
            
	public int sum(int n) {
		int sum =0;
		for (int i = 0; i <= n; i++) {
			sum += i;
		}
		
		return sum;
	}

}


在webservices包下再新建 web service   service name :mywebservice   service erface: test.IAccount//手动选择导入test包下IAccount接口   service : test.AccountImp//业务实现类   //协议为soap协议MyEclipse下配置保持不变   自动生成services.xml配置如下 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
	<service>
		<name>SumService</name>
		<serviceClass>cn.ws.SumService</serviceClass>
		<implementationClass>cn.ws.SumServiceImpl</implementationClass>
		<style>wrapped</style>
		<use>literal</use>
		<scope>application</scope>
	</service>
</beans>


web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <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>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


然后在浏览器测试一下:

输入http://127.0.0.1:8080/WebService/services/SumService?wsdl

结果如下:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions targetNamespace="http://ws.cn" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://ws.cn" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <wsdl:types>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ws.cn">
- <xsd:element name="sum">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="in0" type="xsd:int" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="sumResponse">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="out" type="xsd:int" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
  </xsd:schema>
  </wsdl:types>
- <wsdl:message name="sumResponse">
  <wsdl:part name="parameters" element="tns:sumResponse" /> 
  </wsdl:message>
- <wsdl:message name="sumRequest">
  <wsdl:part name="parameters" element="tns:sum" /> 
  </wsdl:message>
- <wsdl:portType name="SumServicePortType">
- <wsdl:operation name="sum">
  <wsdl:input name="sumRequest" message="tns:sumRequest" /> 
  <wsdl:output name="sumResponse" message="tns:sumResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="SumServiceHttpBinding" type="tns:SumServicePortType">
  <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="sum">
  <wsdlsoap:operation soapAction="" /> 
- <wsdl:input name="sumRequest">
  <wsdlsoap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="sumResponse">
  <wsdlsoap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="SumService">
- <wsdl:port name="SumServiceHttpPort" binding="tns:SumServiceHttpBinding">
  <wsdlsoap:address location="http://127.0.0.1:8080/WebService/services/SumService" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>



这样就说明发布成功了!

 

在客户端调用服务:

新建web工程WebServiceClientTest  

由于在MyEclipse中只有新建webservice才会自动导入xfire所需jar包   所以使用时自己手动导入把jar包考入lib文件夹下

由于XFire机制先要建立跟要接口同样接口名并包含所需思路方法才可以相应思路方法:

public interface SumService {
	public int sum(int n);
}


创建测试类:ClientServiceTest.java

package cn.ws.client;

import java.util.Scanner;

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 ClientServiceTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//创建服务接口模版
		Service serviceModel = new ObjectServiceFactory().create(SumService.class);
		//创建代理工厂
		XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
		//定义Web服务地址
		String url = "http://127.0.0.1:8080/WebService/services/SumService";
		try {
			//根据接口模版和服务地址返回服务类
			SumService sumService = (SumService)factory.create(serviceModel,url);
			Scanner input = new Scanner(System.in);
			System.out.println("请输入任意一个整数");
			int num = input.nextInt();
			//调用服务得到测试结果
			int result = sumService.sum(num);
			System.out.println("1到"+num+"的和为"+result);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值