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里,目录结果如下:

IDEA中用xfire建立webservice_第1张图片

修改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包,目录结构如下:

IDEA中用xfire建立webservice_第2张图片

编写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();
    }
}





你可能感兴趣的:(webservice)