spring+xfire实现webservice简单例子

2023-12-17 12:32

本文主要是介绍spring+xfire实现webservice简单例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

spring xfire是一种比较简单的webservcie方式,下面的步骤描述一个例子。
基本流程是:
1.加载spring,xfire jar文件
2.web.xml加载applicationContext.xml,xfire
3.创建java服务工程,创建远程对外服务接口,实现类,model对象
4.在applicationContext.xml中部署服务
5.测试webservice

 

1.创建一个web工程,加入Maven,pom.xml代码见:

<dependencies><dependency><groupId>org.codehaus.xfire</groupId><artifactId>xfire-aegis</artifactId><version>1.2.4</version></dependency><dependency><groupId>org.codehaus.xfire</groupId><artifactId>xfire-spring</artifactId><version>1.2.4</version></dependency><dependency><groupId>xalan</groupId><artifactId>xalan</artifactId><version>2.7.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>2.0</version></dependency></dependencies>

 
如果没有使用Maven,将如下jar文件加载到classpath下面:
activation-1.1.jar
aopalliance-1.0.jar
avalon-framework-4.1.3.jar
commons-attributes-api-2.1.jar
commons-beanutils-1.7.0.jar
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.1.jar
jaxen-1.1-beta-9.jar
jdom-1.0.jar
junit-3.8.1.jar
log4j-1.2.12.jar
logkit-1.0.1.jar
mail-1.4.jar
qdox-1.5.jar
servlet-api-2.3.jar
spring-1.2.6.jar
spring-aop-2.0.jar
spring-beans-2.0.jar
spring-context-2.0.jar
spring-core-2.0.jar
spring-web-2.0.jar
stax-api-1.0.1.jar
stax-utils-snapshot-20040917.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.0.jar
xalan-2.7.0.jar
xbean-2.2.0.jar
xbean-spring-2.7.jar
xercesImpl-2.6.2.jar
xfire-aegis-1.2.4.jar
xfire-annotations-1.2.4.jar
xfire-core-1.2.4.jar
xfire-spring-1.2.4.jar
xfire-xmlbeans-1.2.4.jar
xml-apis-1.0.b2.jar
xmlParserAPIs-2.6.2.jar
XmlSchema-1.1.jar

 

2.编辑web.xml

<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>xfire</servlet-name><servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class></servlet><servlet-mapping><servlet-name>xfire</servlet-name><url-pattern>/service/*</url-pattern></servlet-mapping>

 

3.创建java服务工程,在web工程中关联该java工程或将java服务工程打包jar放到web工程classpath下面。
服务对外接口:org.simpledev.spring.xfire.service

HelloServiceString sayHello(String username) throws RemoteException;User listUserById(int id) throws RemoteException;

 

接口实现类:org.simpledev.spring.xfire.service.impl

HelloServiceImpl implements HelloServicepublic User listUserById(int id) throws RemoteException {System.out.println("server listUserById invoke");User user = new User(id,"纪纱","女",12,"日本都立海原高校");return user;}public String sayHello(String username) throws RemoteException {System.out.println("server sayHello invoke");return "你好 " + username;}

 

model对象
  属性:id,username,sex,age,address
  提供构造函数
  每个属性提供getXXX(),setXXX()方法

 

4.编辑applicationContext.xml
 

<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">  <property name="serviceFactory" ref="xfire.serviceFactory" />  <property name="xfire" ref="xfire" /> </bean> <bean id="userWS" class="org.simpledev.spring.xfire.service.impl.HelloServiceImpl"></bean> <bean id="userService" parent="baseWebService">  <property name="serviceBean" ref="userWS" />  <property name="serviceClass" value="org.simpledev.spring.xfire.service.HelloService" /> </bean>

 

测试部署是否成功,发布web工程,在IE地址栏中输入: http://127.0.0.1:8080/springxfire/service/,出现链接后: http://127.0.0.1:8080/springxfire/service/HelloService?wsdl
表示部署成功

 

5.测试工程

public static void main(String[] args) {try {Service serviceModel = new ObjectServiceFactory().create(HelloService.class);HelloService service = (HelloService) new XFireProxyFactory().create(serviceModel,"http://127.0.0.1:8080/springxfire/service/HelloService");//方法一 测试String str = service.sayHello("世界");System.out.println(str);//方法二 测试User user = service.listUserById(1);System.out.println("id:"+user.getId());System.out.println("username:"+user.getUsername());System.out.println("sex:"+user.getSex());System.out.println("age:"+user.getAge());System.out.println("address:"+user.getAddress());} catch (MalformedURLException e) {e.printStackTrace();} catch (RemoteException e) {e.printStackTrace();}}

 
 发现的问题:在实际项目中,
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>2.0</version>
    </dependency>一些jar包与原来项目中的spring jar可能会产生冲突,可以将spring-web jar去掉或覆盖,如下:
(
aopalliance-1.0.jar
avalon-framework-4.1.3.jar
commons-logging-1.1.jar
log4j-1.2.12.jar
logkit-1.0.1.jar
servlet-api-2.3.jar
spring-aop-2.0.jar
spring-beans-2.0.jar
spring-context-2.0.jar
spring-core-2.0.jar
spring-web-2.0.jar
)
其次web.xml中注意加载:xfire,否则测试是否部署成功时,http://.../service/,其实请求的就是在web.xml中配置的servlet

这篇关于spring+xfire实现webservice简单例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/504375

相关文章

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

JavaScript中的isTrusted属性及其应用场景详解

《JavaScript中的isTrusted属性及其应用场景详解》在现代Web开发中,JavaScript是构建交互式应用的核心语言,随着前端技术的不断发展,开发者需要处理越来越多的复杂场景,例如事件... 目录引言一、问题背景二、isTrusted 属性的来源与作用1. isTrusted 的定义2. 为

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫