zoukankan      html  css  js  c++  java
  • 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

    2.使用maven来构建:

      废话不多说:

    目录结构如下:

    pom.xml 如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>webMaven</groupId>
        <artifactId>webMaven</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>webMaven</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.codehaus.xfire</groupId>
                <artifactId>xfire-generator</artifactId>
                <version>1.2.6</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.3</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>generate-sources</phase>
                            <configuration>
                                <tasks>
                                    <!-- START SNIPPET: client -->
                                    <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"
                                        classpathref="maven.compile.classpath" />
    
                                    <wsgen outputDirectory="${basedir}/src/main/java"
                                        wsdl="${basedir}/src/wsdl/test.wsdl" package="cn.c.jorcen.test"
                                        overwrite="true" />
                                    <!-- END SNIPPET: client -->
                                </tasks>
                                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.5</source>
                        <target>1.5</target>
                    </configuration>
                </plugin>
            </plugins>
            <sourceDirectory>src/main</sourceDirectory>
            <testSourceDirectory>src/test</testSourceDirectory>
            <resources>
                <resource>
                    <directory>src/main</directory>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.xsd</include>
                    </includes>
                </resource>
            </resources>
            <testResources>
                <testResource>
                    <directory>src/test</directory>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                        <include>**/*.wsdl</include>
                    </includes>
                </testResource>
            </testResources>
        </build>
    
        <!--  needed for XmlSchema -->
        <repositories>
            <repository>
                <id>codehaus</id>
                <name>Codehaus maven repository</name>
                <url>http://dist.codehaus.org/</url>
                <layout>legacy</layout>
            </repository>
        </repositories>
    
    
    </project>

    run:

     或者在工程根目录下: mvn clean generate-sources

     控制台输出如下:

    [INFO] Scanning for projects...
    [WARNING] 
    [WARNING] Some problems were encountered while building the effective model for webMaven:webMaven:jar:0.0.1-SNAPSHOT
    [WARNING] 'repositories.repository.layout = legacy' is deprecated: codehaus @ webMaven:webMaven:0.0.1-SNAPSHOT, F:WorkspacesMyEclipse8.6workspace-webServicewebMavenpom.xml
    [WARNING] 
    [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
    [WARNING] 
    [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
    [WARNING] 
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building webMaven 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-antrun-plugin:1.3:run (default) @ webMaven ---
    [INFO] Executing tasks
    2014-4-15 11:12:36 org.codehaus.xfire.gen.Wsdl11Generator generate
    信息: Generating code for WSDL at file:/F:/Workspaces/MyEclipse8.6/workspace-webService/webMaven/src/wsdl/test.wsdl with a base URI of file:/F:/Workspaces/MyEclipse8.6/workspace-webService/webMaven/src/wsdl/test.wsdl
    2014-4-15 11:12:38 org.codehaus.xfire.gen.jsr181.AbstractServiceGenerator generate
    信息: Creating class cn.c.jorcen.test.CalculatorServicePortType
    2014-4-15 11:12:38 org.codehaus.xfire.gen.jsr181.AbstractServiceGenerator generate
    信息: Creating class cn.c.jorcen.test.CalculatorServiceImpl
    cncjorcenserviceAdd.java
    cncjorcenserviceAddResponse.java
    cncjorcenserviceDel.java
    cncjorcenserviceDelResponse.java
    cncjorcenserviceGet.java
    cncjorcenserviceGetResponse.java
    cncjorcenserviceObjectFactory.java
    cncjorcenservicepackage-info.java
    cncjorcen	estCalculatorServiceClient.java
    cncjorcen	estCalculatorServiceImpl.java
    cncjorcen	estCalculatorServicePortType.java
    [INFO] Executed tasks
    [INFO] Registering compile source root F:WorkspacesMyEclipse8.6workspace-webServicewebMavensrcmainjava
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 5.436s
    [INFO] Finished at: Tue Apr 15 11:12:38 CST 2014
    [INFO] Final Memory: 2M/5M
    [INFO] ------------------------------------------------------------------------

    结果如下:

    功成身退!

  • 相关阅读:
    2019.4.1 JMeter中文乱码解决方案
    19.3.25 sql查询语句
    2019.3.23 python的unittest框架与requests
    2019.3.22 JMeter基础操作
    19.3.21 计算机网络基础知识
    19.3.20 cmd操作:1.dir查看当前文件夹内的文件;2.alt+space+c关闭cmd窗口
    19.3.20 解决pycharm快捷键无法使用问题和熟悉git与码云操作流程
    19.3.19 使用Flask框架搭建一个简易登录服务器
    回调函数
    var img = new Image()
  • 原文地址:https://www.cnblogs.com/mjorcen/p/3663768.html
Copyright © 2011-2022 走看看