xfire webservice spring 集成实例

xfire webservice spring 集成实例

1.打开  myeclipse 60 点击 file -->new -->web service project
Project name  为 xfirewebservice --》 finash

2.src目录下创建 包和相应的 java文件 如下所示:创建接口文件:
package com.xino.service;

public interface IHelloWS {
    public String sayHello(String sb);
}
创建实现文件 :
package com.xino.service;

public class HelloWSImpl implements IHelloWS {

    public String sayHello(String sb) {
        // TODO Auto-generated method stub
        return"Hello "+sb;
    }

}

service.xml文件可以省略了, web服务定义在了 XFireServlet-servlet.xml 文件中

3. 目录webroot 下的目录web-inf目录下创建 applicationContext.xml 文件
这个是 spring的配置文件 如果使用其它的配置文件,可以将下面的 bean添加到那个配置文件中
内容如下 :
com.xino.service.HelloWSImpl 为包 com.xino.service 下的类HelloWSImpl (服务的实现类)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="mathBean" class="com.xino.service.HelloWSImpl" />
</beans>

mathBean 这个Bean就是我们的实现类,也可以在这个文件中定义其它的需要spring管理的bean


在目录webroot 下的目录web-inf目录下创建 XFireServlet-servlet.xml 文件
根据spring 规范 ,这个文件名起 XFireServlet-servlet.xml 其中的XFireServlet 是web.xml配置的 DispatcherSerlet的名称
web.xml 中的相应内容如下:
<servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
            <!--  org.codehaus.xfire.spring.XFireSpringServlet   -->
        </servlet-class>
    </servlet>
XFireServlet-servlet.xml  文件 内容如下 :
<entry key="/MathService">  为服务名称
<value>com.xino.service.IHelloWS</value> 定义的接口
这个文件上半部分将mathservice这个url 和math这个bean联系在一起
下半部分定义了web服务的 bean和服务接口,其中 mathbean是我们在applicationContext.xml 文件中
配置的那个bean


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/MathService">
                    <ref bean="math" />
                </entry>
            </map>
        </property>
    </bean>
    <bean id="math"
        class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <property name="serviceFactory">
            <ref bean="xfire.serviceFactory" />
        </property>
        <property name="xfire">
            <ref bean="xfire" />
        </property>
        <property name="serviceBean">
            <ref bean="mathBean" />
        </property>
        <property name="serviceClass">
            <value>com.xino.service.IHelloWS</value>
        </property>
    </bean>
</beans>

编辑 web.xml文件 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:org/codehaus/xfire/spring/xfire.xml
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <!--  -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <!--
        <servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>
        org.codehaus.xfire.transport.http.XFireConfigurableServlet
        </servlet-class>
        <load-on-startup>0</load-on-startup>
        </servlet>
    -->
    <servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
            <!--  org.codehaus.xfire.spring.XFireSpringServlet   -->
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/servlet/XFireServlet/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
        <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

这个文件需要注意的三个部分
1 contextConfigLocation 部分需要有 classpath:org/codehaus/xfire/spring/xfire.xml
2 定义 listener   org.springframework.web.context.ContextLoaderListener
3 定义  DispatcherServlet    XFireServlet

其中的各个  servlet-mapping  根据自己习惯来


4. 部署 webservice
在浏览器 输入 http://localhost:8080/xfirewebservicespring/services/MathService?wsdl

出现如下提示  (部分):
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="http://service.xino.com" xmlns:tns="http://service.xino.com" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
- <wsdl:types>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.xino.com">
- <xsd:element name="sayHello">
- <xsd:complexType>

表示 服务 启动成功, 至此 服务部署完成




测试 服务 :
项目中 需要引入  commons-httpclient-3.0.1.jar包 拷贝至 webroot下的 web-inf目录下的 lib目录中 即可
1. 编写 接口文件
package com.xino.service.client;

public interface IHelloWS {
    public String sayHello(String sb);
}

2. 编写调用 文件
package com.xino.service.client;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;



public class Client  {
    public static void main(String[] args) {
        // TODO 自动生成方法存根

        Service srvcModel = new ObjectServiceFactory().create(IHelloWS.class);
        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
                .newInstance().getXFire());
//xfirewebservicespring 为我自己的项目名称  MathService  服务名称
        String helloWorldURL = "http://localhost:8080/xfirewebservicespring/MathService";
        try {
            IHelloWS srvc = (IHelloWS) factory.create(srvcModel, helloWorldURL);
            System.out.println("调用函数 sayHello得到结果 :" + srvc.sayHello("xujinpeng"));
            // srvc.add(124, 456);
            //System.out.println("调用函数 add得到结果 :" + srvc.add(1516, 1515));
            //System.out.println("调用函数 minus得到结果 :" + srvc.minus(1516, 1515));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值