web service 的简单实现

Java高级群1群:224651178

Java高级群1群:134787504


如何使用webservices

1.创建一个项目 webServer

  项目结构图

  web service 的简单实现_第1张图片

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

项目结果为下图

 web service 的简单实现_第2张图片

 

该项目下也必须创建一个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



你可能感兴趣的:(web service 的简单实现)