2008年3月27日星期四

昨天用xfire写了第一个webservice

忙里偷闲
研究了一下webservice,做了个hello world
一点理解,xfire能把一个普通java类映射成web服务
首先要建立一个供外部调用的接口interface
然后建立一个类实现这个接口
再然后在service.xml中配置web服务的名字,名称空间,接口类名,实现接口的类名,这个service.xml文件要放在src/META-INF/xfire/services.xml
最后在web.xml中配置xfire 的servlet
OK了
===============接口HelloWorldService 类==================
package com.capinfo.xfire.pojo;
/**
* @author sing
* @explain:
* @datetime:Mar 26, 2008 4:48:19 PM
*/

public interface HelloWorldService {

    public String sayHello();
   
}
==============接口实现类HelloWorldServiceImpl =====================
package com.capinfo.xfire.pojo;
/**
* @author sing
* @explain:
* @datetime:Mar 26, 2008 4:50:15 PM
*/
public class HelloWorldServiceImpl implements HelloWorldService {

    public String sayHello() {
       
        return "this.is.my first web service";
    }

}
===============service.xml==================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
    <name>HelloWorldService</name>
    <namespace>http://capinfo.com/HelloWorldService</namespace>
    <serviceClass>
        com.capinfo.xfire.pojo.HelloWorldService
    </serviceClass>
    <implementationClass>
        com.capinfo.xfire.pojo.HelloWorldServiceImpl
    </implementationClass>
</service>
</beans>
==============web.xml=======================
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
===================Test.java========================
package com.capinfo.xfire.pojo;

import java.net.MalformedURLException;

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

/**
* @author spring
* @explain:
* @datetime:Mar 26, 2008 5:50:02 PM
*/
public class Test {

    public static void main(String [] args){
        Service serviceModel = new ObjectServiceFactory().create(HelloWorldService.class);
        try {
            HelloWorldService service = (HelloWorldService)
                new XFireProxyFactory().create(serviceModel, "http://localhost:8081/xfire/services/HelloWorldService");
            System.out.println(service.sayHello());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

======================放jar包========================
"activation-1.1.jar"
"commons-codec-1.3.jar"
"commons-httpclient-3.0.jar"
"commons-logging-1.0.4.jar"
"jaxen-1.1-beta-9.jar"
"jdom-1.0.jar"
"mail-1.4.jar"
"spring-1.2.6.jar"
"stax-api-1.0.1.jar"
"wsdl4j-1.6.1.jar"
"wstx-asl-3.2.0.jar"
"xbean-spring-2.8.jar"
"xfire-all-1.2.6.jar"


参考资源:http://www.blogjava.net/fastzch/archive/2008/01/03/172535.html

没有评论:

发表评论