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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个