java 舍入误差 博客_HIMMEL

使用XFire与Spring开发Web Service

源代码下载(三个样例工程Maven版本) xfire-src.zip

相关lib文件请到XFire官方网站(见参考) 下载发布版本。

实现功能与特点

基于J2EE平台的Web Service服务

开发方便,配置简单

设计接口

实现服务

配置暴露接口

XFire将自动生成对应的wsdl

支持高级详细配置

与Spring无缝集成

运行环境

JDK 1.4+

Tomcat 4.0+ / WebLogic 8.1 (需要特殊配置,见附录)未测试其他环境

开发平台

实施步骤

概述

建立一个Java Project用于Domain实体和Web Service接口,这个工程的输出是一个jar包,为Web Service工程和Web Service Client共享。

建立WTP动态WEB工程部署Web Service

建立Java Project使用Web Service (Web Service Client)

新建Java Project

建立普通Java Project 以下称为common工程

如果使用Maven,项目依赖

javax.mail

mail

1.4

普通情况需要加入如下lib

activation-1.1.jar

mail-1.4.jar

编写类 org.ave7.xfire.domain.User

org.ave7.xfire.domain.User.java

packageorg.ave7.xfire.domain;importjava.io.Serializable;importjava.util.Date;publicclassUserimplementsSerializable {privatestaticfinallongserialVersionUID=6517808321041980976L;privateLong userId;privateString accountId;privateString userName;privateDate lastLogin;publicString getAccountId() {returnaccountId;

}publicvoidsetAccountId(String accountId) {this.accountId=accountId;

}publicDate getLastLogin() {returnlastLogin;

}publicvoidsetLastLogin(Date lastLogin) {this.lastLogin=lastLogin;

}publicLong getUserId() {returnuserId;

}publicvoidsetUserId(Long userId) {this.userId=userId;

}publicString getUserName() {returnuserName;

}publicvoidsetUserName(String userName) {this.userName=userName;

}

}

编写接口 org.ave7.xfire.ws.UserService

org.ave7.xfire.ws.UserService.javapackageorg.ave7.xfire.ws;importorg.ave7.xfire.domain.User;publicinterfaceUserService {publicUser queryUserByAccoutId(String accountId);publicvoidcreateUser(User user);

}

新建WTP动态WEB工程

配置WTP加入Tomcat Server Runtime

选择Windows -> Preference

在左侧选择Server->Installed Runtime 添加Tomcat

081607-0426-xfirespring1.png

新建工程,选择Dynamic Web Project 输入Project Name: XFireService

081607-0426-xfirespring2.png

设置运行环境为Dynamic Web Module 2.3 , JDK 1.4

081607-0426-xfirespring3.png

如果需要结合Maven,请如下配置,否则使用默认即可

081607-0426-xfirespring4.png

如果使用Maven请注入如下依赖以及之前的Common工程,否则根据后面的列表复制lib文件到对应目录

org.codehaus.xfire

xfire-aegis

1.2.4

org.codehaus.xfire

xfire-spring

1.2.4

xalan

xalan

2.7.0

Lib文件列表

xbean-2.2.0.jar

qdox-1.5.jar

commons-attributes-api-2.1.jar

ant-1.5.jar

wstx-asl-3.2.0.jar

stax-utils-20040917.jar

commons-httpclient-3.0.jar

spring-1.2.6.jar

XmlSchema-1.1.jar

stax-api-1.0.1.jar

jaxen-1.1-beta-9.jar

xfire-annotations-1.2.4.jar

xfire-aegis-1.2.4.jar

xfire-core-1.2.4.jar

xfire-spring-1.2.4.jar

xfire-xmlbeans-1.2.4.jar

jdom-1.0.jar

xbean-spring-2.7.jar

wsdl4j-1.6.1.jar

commons-beanutils-1.7.0.jar

activation-1.1.jar

mail-1.4.jar

commons-codec-1.3.jar

xmlParserAPIs-2.6.2.jar

xercesImpl-2.6.2.jar

junit-3.8.1.jar

commons-logging-1.0.4.jar

xalan-2.7.0.jar

实现服务

org.ave7.xfire.ws.UserServiceImpl.java

packageorg.ave7.xfire.ws;importjava.rmi.RemoteException;importjava.util.Date;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.ave7.xfire.domain.User;publicclassUserServiceImplimplementsUserService {privatestaticfinalLog log=LogFactory.getLog(UserServiceImpl.class);publicvoidcreateUser(User user)throwsRemoteException {

log.debug(“createUser user=”+user);

}publicUser queryUserByAccoutId(String accountId)throwsRemoteException {

log.debug(“accountId=”+accountId);

User user=newUser();

user.setAccountId(“testAccount”);

user.setLastLogin(newDate());

user.setUserName(“测试用户“);

user.setUserId(newLong(123L));returnuser;

}

}

利用Spring、XFire装配与暴露服务

在web.xml配置相关组件

<?xml  version=“1.0″ encoding=“UTF-8″?>web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”

“http://java.sun.com/dtd/web-app_2_3.dtd”>XFireServicecontextConfigLocationclasspath:applicationContext*.xmlorg.springframework.web.context.ContextLoaderListenerxfireorg.codehaus.xfire.spring.XFireSpringServletxfire/service/*

在applicationContext.xml配置相关服务

<?xml  version=“1.0″ encoding=“UTF-8″?>beans PUBLIC “-//SPRING//DTD BEAN//EN”

“http://www.springframework.org/dtd/spring-beans.dtd”>

测试并访问服务

检查lib文件夹,Maven用户注意添加对common工程的依赖,其他情况请把common工程导出的jar包复制到web-inf/lib目录

在WTP中添加服务器,部署应用

081607-0426-xfirespring5.png

在WTP中利用Web Service Explore测试SOAP方式,在WTP工具栏选择

081607-0426-xfirespring8.png 最右侧按钮

点击右上角进入WSDL page

081607-0426-xfirespring9.png

点击WSDL Main节点输入服务wsdl地址,点击

081607-0426-xfirespring10.png

列出所有的可用服务,选择服务测试

081607-0426-xfirespring11.png

测试queryUserByAccountId

081607-0426-xfirespring12.png

点击结果中的Source可以看到SOAP报文

081607-0426-xfirespring13.png

利用XFire实现客户端

新建一个普通Java工程,lib中引用XFire相关包

如果使用Maven添加如下依赖以及对common工程的引用

org.codehaus.xfire

xfire-aegis

1.2.4

普通Java工程添加如下lib以及对common工程的引用

activation-1.1.jar

commons-codec-1.3.jar

commons-httpclient-3.0.jar

commons-logging-1.0.4.jar

jaxen-1.1-beta-9.jar

jdom-1.0.jar

junit-3.8.1.jar

mail-1.4.jar

stax-api-1.0.1.jar

stax-utils-20040917.jar

wsdl4j-1.6.1.jar

wstx-asl-3.2.0.jar

xercesImpl-2.6.2.jar

xfire-aegis-1.2.4.jar

xfire-core-1.2.4.jar

xmlParserAPIs-2.6.2.jar

XmlSchema-1.1.jar

编写客户端程序

org.ave7.xfire.client.MyClient.java

packageorg.ave7.xfire.client;importjava.net.MalformedURLException;importjava.rmi.RemoteException;importorg.ave7.xfire.domain.User;importorg.ave7.xfire.ws.UserService;importorg.codehaus.xfire.client.XFireProxyFactory;importorg.codehaus.xfire.service.Service;importorg.codehaus.xfire.service.binding.ObjectServiceFactory;publicclassMyClient {publicstaticvoidmain(String[] args) {try{

Service serviceModel=newObjectServiceFactory()

.create(UserService.class);

UserService service=(UserService)newXFireProxyFactory().create(

serviceModel,

“http://localhost:8080/XFireService/service/UserService”);User user=service.queryUserByAccoutId(“123″);

System.out

.println(“userId=”+user.getUserId()+“, userName=”+user.getUserName()+“, lastLogin=”+user.getLastLogin());

}catch(MalformedURLException e) {

e.printStackTrace();

}catch(RemoteException e) {

e.printStackTrace();

}

}

}

运行。控制台打印输出

2007-8-16 10:25:52 org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry crea

teTypeCreator

信息: Couldn’t find Java 5 module on classpath. Annotation mappings will not be su

pported.

userId=123, userName=测试用户, lastLogin=Thu Aug 16 10:25:53 CST 2007

扩展主题

利用MTOM传输二进制对象(文件)

MTOM是XFire的一个组件,可以协助利用压缩等手段更快的在网络上传输base64的SOAP对象

MTOM以及aegis默认支持如下几种java对象的数据

byte[]

javax.activation.DataHandler

javax.activation.DataSource

配置启用MTOM

在Spring配置文件中需要MTOM的服务中加入如下配置

在客户端编写传输程序

Service serviceModel=newObjectServiceFactory()

.create(UserService.class);

UserService service=(UserService)newXFireProxyFactory().create(

serviceModel,

“http://localhost:8080/XFireService/service/UserService”);Client client=Client.getInstance(service);

client.setProperty(“mtom-enabled”, “true”);

client.setProperty(HttpTransport.CHUNKING_ENABLED, “true”);

User user=service.queryUserByAccoutId(“123″);

System.out

.println(“userId=”+user.getUserId()+“, userName=”+user.getUserName()+“, lastLogin=”+user.getLastLogin());

ByteArrayDataSource byteData=newByteArrayDataSource(

“Test Sending Byte”.getBytes(), “text/plain; charset=UTF-8″);

service.transferDataSource(byteData);

附录

XFire在WebLogic 8.1中的配置

Add the QName JAR file (qname.jar) into the WEB-INF/lib folder in your WAR file

Add a file weblogic.xml into the WEB-INF folder in your WAR file

weblogic-web-app PUBLIC

“-//BEA Systems, Inc.//DTD Web Application 8.1//EN”

“http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd”>true

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值