WebService-----Xfire

Xfire相对Axis2 个人感觉相对简单一点,因为myEclipse上可以自动生成。而且在目前来讲,Xfire也是更受欢迎一点。

首先说明Xfire所用jar包:http://download.csdn.net/detail/u014104269/9028679

下面是步骤:

步骤一:打开myEclipse ,New-》web Service project

写上name ,选择Xfire ,就直接Finish 就好了。

然后在项目上,NEw-》other-》web Service

Finish

 此时打开services.xml 发现其中多了些东西:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">

    <service>
        <name>MyService</name>
        <serviceClass>com.demo.service.IMyService</serviceClass>
        <implementationClass>
            com.demo.service.MyServiceImpl
        </implementationClass>
        <style>wrapped</style>
        <use>literal</use>
        <scope>application</scope>
    </service></beans>

多了两个文件 一个是接口文件:

package com.demo.service;
//Generated by MyEclipse

public interface IMyService {
	
	public String example(String message);
	
}

  另一个是对该接口的实现:

package com.demo.service;
//Generated by MyEclipse

public class MyServiceImpl implements IMyService {
	
	public String example(String message) {
		return message;
	}
	
}

  此时:

使用client代码就可以调用了:

package com.demo.client;

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

import com.demo.service.IMyService;

public class Test {
    /**
     * @param args
*/
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String serviceUrl = "http://localhost:8081/Demo/services/MyService";
        Service serviceModel = new ObjectServiceFactory().create(IMyService.class, null, "http://localhost:8081/Demo/services/MyService?wsdl", null);
        XFireProxyFactory serviceFactory = new XFireProxyFactory();
        try{
            IMyService service = (IMyService)serviceFactory.create(serviceModel,serviceUrl);
            String hello = service.example("cys");
            System.out.println(hello);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

  另一种方法是 使用JDK中lib文件夹下的一个叫做 wsimport.exe的文件

把在该文件夹下生成的文件全部导入到项目中 就可以了:

客户端代码是:

package com.demo.client;

public class T {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyService service =new MyService();
		MyServicePortType spt = service.getMyServiceHttpPort();
		System.out.println(spt.example("haahahha"));
	}

}

  不过在使用第二种方法时候,需要删除jar包中的xfire-all ,否则会报错。

原文地址:https://www.cnblogs.com/caoyusongnet/p/4745569.html