Myeclipse spring xfire 开发webservice 详解

本文介绍如何使用MyEclipse、Spring和XFire快速搭建WebService应用,并部署到Tomcat服务器。文中详细展示了创建项目、配置环境、编写代码和服务发布的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

Myeclipse  spring  xfire  开发 webservice

 

XFire是一个简化WebService开发的开源项目,通过SpringXFire的结合可以大大简化基于Spring Framework的应用中的WebService开发。

 

一:开发环境

Myeclipse 

MyEclipse  Enterprise Workbench Version: 7.0 Milestone-1

spring  spring 2.5

xfire  1.2

tomcat6.0

 

二: 新建 web service 工程

 

从菜单按照下面的路径依次进入:

 

file new project myeclipse web project

 

之后,到了下面的页面。在这页面填入web 项目的名称:Hello

 

 

 

2 加入 spring 的包

 

myeclipseproject capabilities add spring capabilities 进入下面的页面


 

 

3 加入 xfire 的包

 

myeclipseproject capabilities add xfire capabilities 进入下面的页面


 

 

 

servlet class 选项选择 org.codehaus.xfire.spring.XfireSpringServlet 类,而不是选择默认的org.codehaus.xfire.transport.http.XfireConfigurableServlet

之后,自动在web配置文件中加入了xfireservlet配置,如下:

 

<?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.spring.XFireSpringServlet</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>

 

web配置文件中加入spring监听器,如下:

<context-param>

        <param-name>contextConfigLocation</param-name>

       <param-value>

            classpath:org/codehaus/xfire/spring/xfire.xml,classpath:applicationContext.xml

       </param-value>

    </context-param>

<listener>

 <listener-class>

   org.springframework.web.context.ContextLoaderListener

 </listener-class>

</listener>  

 

  编写代码

 

(1)       接口类

 

package com.imti;

 

public interface Hello {

     public String sayHello(String sb);

 

}

 

(2)       实现类

 

package com.imti;

 

public class HelloWS implements Hello{

     

    public String sayHello(String somebody) {

 

           return"你好: "+somebody;

 

        }

 

 

}

 

配置服务

 

将上文中实现的服务,加入到spring的配置文件中,代码如下:

 

  <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>

    

     <bean id="helloWS" class="com.imti.HelloWS"/>

 

     <bean name="helloService" class="org.codehaus.xfire.spring.ServiceBean">

 

     <property name="serviceBean" ref="helloWS"/>

 

     <property name="serviceClass" value="com.imti.Hello"/>

 

     <property name="inHandlers">

 

      <list>

 

        <ref bean="addressingHandler"/>

 

      </list>

 

    </property>

 

</bean>

部署服务

 

myeclipse 部署服务,并启动tomcat

从浏览器访问到以下页面:

 

1 http://localhost:8080/Hello/services/helloService?wsdl

 

2 http://localhost:8080/Hello/services/Hello?wsdl

 

 

可能出现的问题

1 spring 配置文件异常:Document root element "beans", must match DOCTYPE root "null"

分析:这表明审判日那个需要XML DTD的设置方式,而项目中很显然这是XML Schema的设置方式。我把spring 配置文件改为XML DTD的设置方式,如下:

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!--<beans

       xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

   

     -->

     <beans>

    

     <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>

    

     <bean id="helloWS" class="com.imti.HelloWS"/>

 

     <bean name="helloService" class="org.codehaus.xfire.spring.ServiceBean">

 

     <property name="serviceBean" ref="helloWS"/>

 

     <property name="serviceClass" value="com.imti.Hello"/>

 

     <property name="inHandlers">

 

      <list>

 

        <ref bean="addressingHandler"/>

 

      </list>

 

    </property>

 

</bean>

</beans>

 

 

webservices 客户端开发

就在本项目开发客户端。

newother myeclipse web services client 进入下面的页面



 

 

选择 xfire 单选项,进入下一步页面。


 

 

在这个页面的WSDL URL 处填入:

 

http://localhost:8080/Hello/services/Hello?wsdl

 

新建客户类所在的包:com.imti.client。点击下一步完成。

IDE中可以看出,总共生成6个类。

 


 

 

 

最后,加入代码:


helloClient.java文件的的main函数中加入:

     System.out.println(service.sayHello("tech fan"));

   

helloClient.java文件的的main函数完整的代码如下:

public static void main(String[] args) {

       

 

        HelloClient client = new HelloClient();

       

              //create a default service endpoint

        HelloPortType service = client.getHelloHttpPort();

       

        System.out.println(service.sayHello("tech fan"));

       

              System.out.println("test client completed");

                      System.exit(0);

      }

将项目作为Application编译执行,

你好: tech fan

test client completed

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值