码迷,mamicode.com
首页 > Web开发 > 详细

XFire WebService使用实例

时间:2015-03-02 19:14:35      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:xfire   webservice   

几年前搞过,当时只是匆匆的用。相隔几年之后再用时有些模糊了,参考了别人的例子实现了,特此总结下。

一、要使用先下载:http://xfire.codehaus.org/Download

二、先说服务端:

       1、新建JavaWeb项目,将jar包拷进webroot/lib下面;(xfire-all-1.2.6.jar和xfire-distribution-1.2.6\xfire-1.2.6\lib下面的所以jar都拷)

        2、在WebRoot/META-INF下面新建文件夹xfire;

       3、在xfire文件夹下面新建services.xml文件

    内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- <beans>
    <service xmlns="http://xfire.codehaus.org/config/1.0">
      <name>Hello</name>
      <namespace>http://com.qh.helloWorld/Hello</namespace>
      <serviceClass>com.qh.helloWorld.Hello</serviceClass>
    </service>
</beans> -->
<beans xmlns="http://xfire.codehaus.org/config/1.0">
  <service>
    <name>KingTeamService</name>
    <namespace>http://com.yao.xfire.service/KingTeamService</namespace>
    <serviceClass>com.yao.xfire.service.MathService</serviceClass>
    <implementationClass>com.yao.xfire.service.MathServiceImpl</implementationClass>
  </service>
</beans>

4、在web.xml文件里面添加service拦截

内容如下:

<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>

5、新建KingTeamService.java 接口

内容如下:

package com.yao.xfire.service;

public interface KingTeamService {
	public int add(int a,int b);
}

6、新建KingTeamService.java接口的实现类

内容如下:

package com.yao.xfire.service;

import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;

public class MathServiceImpl implements KingTeamService{

	private final Log log = LogFactory.getLog(MathServiceImpl.class);
	@Override
    public int add(int a, int b) {
        log.info("invoke method add.");
        return a + b;
    }

}

三、调用服务的客户端:

1、MathServiceClient.java

package com.yao.xfire.client;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;

import com.yao.xfire.service.KingTeamService;

public class MathServiceClient {
    private static KingTeamService mathService;
    
    private MathServiceClient(){}
     
    public static KingTeamService getMathService(String serviceUrl) throws MalformedURLException{
        if(mathService == null){
            //创建服务
            Service srvcModel = new ObjectServiceFactory().create(KingTeamService.class);
            //创建XFire对象
            XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
            //调用Web服务
            mathService = (KingTeamService) factory.create(srvcModel, serviceUrl);
             
            //设置客户端调用的属性
            Client client = Client.getInstance(mathService);
            client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "300");
            client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
            client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true");
            //如果需要设置代理
            //client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_HOST, "10.3.1.6" );
            //client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_PORT, "8080");
        }
         
        return mathService;
    }
     
    public static void main(String[] args) throws MalformedURLException{
        KingTeamService service = MathServiceClient.getMathService("http://localhost:80/xfireD/services/MathService");
        int result = service.add(1, 14);
        System.out.println("get the result: " + result);
    }
}


然后将项目放进web服务器里面,运行。然后在浏览器里面输入访问地址进行测试:http://localhost/xfireD/services/KingTeamService?wsdl

运行客户端即可得到计算的结果。

例子虽然简单,但却将使用的过程说的清楚明白。以后可以参考着实现了,哈哈。


 

XFire WebService使用实例

标签:xfire   webservice   

原文地址:http://blog.csdn.net/zhao50632/article/details/44020003

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!