xfire for web-Service

1.0 XFire

XFire是codeHaus组织提供的一个开源框架,它构建了POJOSOA之间的桥梁,主要特性就是支持将POJO通过非常简单的方式发布成Web服务,这种处理方式不仅充分发挥了POJO的作用,简化了Java应用转化为Web服务的步骤和过程,也直接降低了SOA的实xfire现难度,为企业转向SOA架构提供了一种简单可行的方式。

目前,XFire虽然已经放弃维护了,并入了apache 的顶级项目CXF中,但是使用的人还是不少,所以留下笔录.供以后回忆.

下面是官方给出的一段话:
XFire is now CXF
Icon
User‘s looking to use XFire on a new project, should use CXF instead. CXF is a continuation of the XFire project and is considered XFire 2.0. It has many new features, a ton of bug fixes, and is now JAX-WS compliant! XFire will continue to be maintained through bug fix releases, but most development will occur on CXF now. For more information see the XFire/Celtix merge FAQ and the CXF website.

 


1.2 XFire的使用

1.2.1 XFire的使用,自底向上篇;

1.下载Xfire , 地址 http://xfire.codehaus.org/Download

 

2.导入jar包和所有依赖

3.编写接口

package cn.c.jorcen.service;

public interface CalculatorServicer {
    public int add(int a, int b);

    public int del(int a, int b);

    public int get(int a, int b);
}

 

4.编写实现了

package cn.c.jorcen.service.impl;

import cn.c.jorcen.service.CalculatorServicer;

public class CalculatorServicerImpl implements CalculatorServicer {

    public int add(int a, int b) {

        return a + b;
    }

    public int del(int a, int b) {
        // TODO Auto-generated method stub
        return a - b;
    }

    public int get(int a, int b) {
        return a - b;
    }

    public static void main(String[] args) {
        System.out.println(CalculatorServicerImpl.class);
        
    }

}

 

5.编辑web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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>
<!--        <display-name>XFire Servlet</display-name>-->
        <servlet-class>
            org.codehaus.xfire.transport.http.XFireConfigurableServlet</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>
</web-app>

 

6.编辑 META-INF/xfire/services.xml

<!-- START SNIPPET: services -->
<beans xmlns="http://xfire.codehaus.org/config/1.0">
  <service>
    <name>CalculatorService</name>
    <serviceClass>cn.c.jorcen.service.CalculatorServicer</serviceClass>
    <implementationClass>cn.c.jorcen.service.impl.CalculatorServicerImpl</implementationClass>
  </service>
</beans>
<!-- END SNIPPET: services -->

 

7.启动服务器,我用的是Tomcat.测试结果;

 http://localhost:8080/web/services

 

内容如下

8.自己编写测试类:

package cn.c.jorcen.service.test;

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;

import cn.c.jorcen.service.CalculatorServicer;

public class Client {
    public static void main(String[] args) throws Throwable {
        Service service = new ObjectServiceFactory()
                .create(CalculatorServicer.class);
        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
                .newInstance().getXFire());
        String url = "http://localhost:8080/web/services/CalculatorService";
        CalculatorServicer cs = (CalculatorServicer) factory.create(service,
                url);
        System.out.println(cs.getClass());
        int add = cs.add(1, 3);
        int del = cs.del(1, 4);
        System.out.println(add);
        System.out.println(del);

    }
}

结果如下:

class $Proxy0
4
-3

1.2.2 XFire的使用,自顶向下篇;

Xfire 是使用Ant,来自动构建的.为了方便编写,我们把Xfire 的jar 包和 依赖包放在一起.并引入工程.

新建builder.xml如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<!-- default 定义一个默认的任务-->
<project name="calculatorServicer" basedir="." default="gen-webservice">
    <!-- 引入属性文件-->
    <property file="buider.properties">
    </property>
    <!-- 定义 jar 文件 -->
    <path id="project-classpath">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <!--定义一个任务 -->
    <target name="gen-webservice">
        <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="project-classpath" />
        <!--outputDirectory 产生源码的位置,  wsdl文件的位置,package  包名,   overwrite 是否覆盖 -->
        <wsgen outputDirectory="${src.dir}" wsdl="${wsdl.dir}" package="cn.c.ws.client" overwrite="true" />

    </target>

</project>

属性文件如下:

#引用ant里边的当前项目的src
src.dir=${basedir}/src
lib.dir=F:/bin/xfire-1.2.6/lib
wsdl.dir=http://localhost:8080/web/services/CalculatorService?wsdl

生产代码如下:

测试类如下:

public class Mail {
    public static void main(String[] args) {
        CalculatorServiceClient client = new CalculatorServiceClient();
        CalculatorServicePortType pro = client.getCalculatorServiceHttpPort();
        System.out.println(pro.add(1, 3));
        System.out.println(pro.del(8, 9));
    }
}

结果如下:

4
-1

 

 

 

 

xfire for web-Service,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。