web service 的简单实现

Java高级群1群:224651178

Java高级群1群:134787504


如何使用webservices

1.创建一个项目 webServer

  项目结构图

 

ItestService.java为测试接口类  给了声明了一个getNameByUid的方法

package com.wzpmt.service;

//Generated by MyEclipse

 

public interface ItestService {

   

    public String getNameByUid(Object uid);

   

}

 

ItestService.java 为测试接口实现类

 

package com.wzpmt.service;

 

import java.util.HashMap;

import java.util.Map;

//Generated by MyEclipse

 

public class TestServiceImpl implements ItestService {

    public TestServiceImpl(){

       map.put(1,"我是1");

       map.put(2,"我是2");

       map.put(3,"我是3");

      

    }

   

    private Map<Object,Object> map=new HashMap<Object, Object>();

    public String getNameByUid(Object uid){

       Object obj=map.get(uid);

       return obj+"";

    }

   

}


 

services.xml 为webService配置文件

 

<?xml version="1.0"encoding="UTF-8"?>

<beans xmlns="http://xfire.codehaus.org/config/1.0">

 

    <service>

       <name>testService</name>

       <serviceClass>com.wzpmt.service.ItestService</serviceClass>

       <implementationClass>

           com.wzpmt.service.TestServiceImpl

       </implementationClass>

       <style>wrapped</style>

       <use>literal</use>

       <scope>application</scope>

    </service>

</beans>


 

 

 

2.创建一个测试项目 webClient

项目结果为下图

 

 

该项目下也必须创建一个ItestService.java的接口与webServer下ItestService.java相对应。

ItestService.java类

package com.wzpmt.service;

//Generated by MyEclipse

 

public interface ItestService {

   

    public String getNameByUid(Object uid);

   

}


 

完成了以上工作就可以创一个测试类进行测试了。

package com.wzpmt.test;

 

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 com.wzpmt.service.ItestService;

 

public class Test {

    public static void main(String[] args) {

       Service serviceModel = new ObjectServiceFactory().create(ItestService.class);      

       XFire xfire = XFireFactory.newInstance().getXFire(); 

       XFireProxyFactory factory = new XFireProxyFactory(xfire);

       String serviceUrl = "http://localhost:8080/webServer/services/testService";

 

       ItestService client = null;

      

       try {

       client = (ItestService)factory.create(serviceModel,serviceUrl);

       } catch (MalformedURLException e) {

       System.out.println( "Client callwebservice has exception: " + e.toString());

       }

 

       System.out.println(client.getNameByUid(3));

 

    }

}


 

这是一个简单的web service实现,有很多不足,望大家指出

Java高级群1群:224651178

Java高级群1群:134787504



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值