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

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与