IDEA中用xfire建立webservice

一、建立一个web application 项目,命名为Service作为服务端,在src目录下新建一个META-INF文件夹里包含xfire/service.xml,把xfire-1.2.6里lib所有jar和xfire-all-1.2.6.jar拷贝到工程lib里,目录结果如下:


修改web.xml文件:

<?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"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>
            org.codehaus.xfire.transport.http.XFireConfigurableServlet
        </servlet-class>
    </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>

修改services.xml文件:

<beans xmlns="http://xfire.codehaus.org/config/1.0">
    <service>
        <!--接口调用的服务名-->
        <name>HelloWorldService</name>
        <namespace>http://xfire.webservice.com/HelloWorldService</namespace>
        <serviceClass>com.xfire.simple.HelloWorldManager</serviceClass>
    </service>
</beans>

编写IHelloWorldManager接口和实现类HelloWorldManager:

package com.xfire.simple;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-3
 * Time: 上午11:11
 * To change this template use File | Settings | File Templates.
 */
public interface IHelloWorld {
    public String add(int a,int b);
}

package com.xfire.simple;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-2-27
 * Time: 下午4:54
 * To change this template use File | Settings | File Templates.
 */
public class HelloWorldManager {
    public long add(int a, int b) {
        System.out.println("Client call HelloWorld.add("+a+","+b+")......");
        return a + b;
    }
}


在浏览器中输入:http://localhost:8080/services/HelloWorldService?wsdl 这里的services对应于web.xml中的

<servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
</servlet-mapping>
HelloWorldService对应于services.xml中的
<name>HelloWorldService</name>

若出现xml文档形式的内容即说明服务端已经建立好,如图:



二、重新建立一个web application项目,命名为Client作为客户端,导入所需要的jar包,目录结构如下:


编写TestClient.java类:

package com.xfire.client;

import org.codehaus.xfire.client.Client;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-3
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class TestClient {
    public static void helloWorld(){
        try {
            System.out.println("================================helloWorld()========================");
            URL url = new URL("http://localhost:8080//services/HelloWorldService?wsdl");
            Client client = new Client(url);
            Object[] results = new Object[0];
            results = client.invoke("add",new Object[]{3,4});
            System.out.println(results[0]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        helloWorld();
    }
}





IDEA中用xfire建立webservice,古老的榕树,5-wow.com

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