xfire

在构建路径中创建 xfire 用户库  把  网上下载的  xfire 文件中的lib 下面的jar文件 放置到  xfire  用户库里面 另外在构建路径中添加  xfire-all-xxx.jar  xalan.jar  

创建服务的接口
Java代码 
package test; 
 
public interface Icreditcard { 
     public void credit(int cardnum,Double total); 

创建服务接口的实现类
Java代码 
package test; 
 
public class creditcardImpl implements Icreditcard { 
 
    public void credit(int cardnum, Double total) { 
        // TODO Auto-generated method stub 
          System.out.println("卡号:"+cardnum+"总共支付"+total); 
    } 
 

在src 下面建 META-INF /xfire/service.xml    service.xml 内容
Xml代码 
<?xml version="1.0" encoding="UTF-8"?> 
<beans> 
<service xmlns="http://xfire.codehaus.org/config/1.0"> 
<!-- 当前服务的名称   --> 
<name>creditcard</name>  
<!--服务的名称空间--> 
<namespace> http://localhost:8080/creditcard</namespace> 
<!--接口--> 
<serviceClass>services.test.Icreditcard</serviceClass> 
<!--实现--> 
<implementationClass>services.test.creditcardImpl</implementationClass> 
</service> 
</beans> 
修改web.xml
Xml代码 
<servlet> 
    <servlet-name>XFireServlet</servlet-name> 
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> 
    <load-on-startup>0</load-on-startup> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>XFireServlet</servlet-name> 
    <url-pattern>/services/*</url-pattern> 
  </servlet-mapping> 
   访问 http://localhost:8080/project/services/服务的名字?wsdl
 




调用 服务

Java代码 
package client; 
 
import java.net.MalformedURLException; 
 
import org.codehaus.xfire.XFire; 
import org.codehaus.xfire.XFireFactory; 
import org.codehaus.xfire.client.XFireProxyFactory; 
import org.codehaus.xfire.service.Service; 
import org.codehaus.xfire.service.binding.ObjectServiceFactory; 
 
import test.Icreditcard; 
 
public class cient { 
    public void doservice(){ 
    //创建服务的元数据 
    Service serviceModel =new ObjectServiceFactory().create(Icreditcard.class); 
    //创建服务的代理 
    XFire xfire =XFireFactory.newInstance().getXFire(); 
    XFireProxyFactory factory =new XFireProxyFactory(xfire); 
   //代理的地址 
    String serviceURL="http://localhost:8080/services/services/creditcard"; 
    Icreditcard client=null; 
    try { 
        client=(Icreditcard)factory.create(serviceModel,serviceURL); 
    //调用服务 
        client.credit(66666666, 100000000000.00);    
    } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
    } 







大小: 20.1 KB

你可能感兴趣的:(xfire)