开发工具:MyEclipse6.0
应用服务器:tomcat6.0
使用Xfire开发服务器端
1、新建webservice工程(web service project):TestWebService
2、创建java代码(服务器的业务逻辑)
1)创建接口IPerson
package com.billy.service;
public interface IPerson {
public void setName(String name);
public void setPassWord(String passWord);
public String getName();
public String getPassWord();
}
2)创建该接口的实现类 PersonImpl
package com.billy.service;
public class PersonImpl implements IPerson{
private String name = null;
private String passWord = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
3)创建接口 IHelloWorldService
package com.billy.service;
import java.util.List;
import com.billy.service.PersonImpl;
//Generated by MyEclipse
public interface IHelloWorldService {
public String example(String message);
public IPerson getPerson();
public List<IPerson> getAllPerson();
}
4)创建该接口的实现类 HelloWorldServiceImpl
package com.billy.service;
import java.util.ArrayList;
import java.util.List;
import com.billy.service.PersonImpl;
//Generated by MyEclipse
public class HelloWorldServiceImpl implements IHelloWorldService {
public String example(String message) {
System.out.println("调用example方法:" + message);
return "hello world,"+message;
}
public IPerson getPerson() {
System.out.println("调用getPerson方法");
IPerson iPerson = new PersonImpl();
iPerson.setName("wangwei");
iPerson.setPassWord("2210746");
return iPerson;
}
public List<IPerson> getAllPerson(){
List list = new ArrayList();
IPerson iPerson = null;
for(int i = 0;i < 3;i++){
iPerson = new PersonImpl();
iPerson.setName("wangwei" + i);
iPerson.setPassWord("2210746" + i*10);
list.add(iPerson);
}
return list;
}
}
5)编辑services.xml(映射类)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>HelloWorldService</name>
<serviceClass>
com.billy.service.IHelloWorldService
</serviceClass>
<implementationClass>
com.billy.service.HelloWorldServiceImpl
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service></beans>
服务器代码完毕:
大至讲解一下以上代码
以上代码向外界公开了1个接口 IHelloWorldService.java
该接口中有3个方法
分别是
public String example(String message);
返回类型为字符串,输入字符为字符串.可将字符串扩展成xml字符,然后在example方法中进行解析,再将解析后的结果以xml形式返回.这是很多项目中通用的处理方式.
public IPerson getPerson();
返回类型为自定义类的实例,可将数据set进此javaBean中返回.
public List<IPerson> getAllPerson();
返回类型为List集合,约束该集合中必须为IPerson的实例对象,可将多个javaBean add进List集合中返回.
3、部署
以上代码写好后,就可以部署进应用服务器中进行测试了.直接选择此项目,add进tomcat就ok了
4、测试
启动tomcat
打开IE
输入http://localhost:8080/TestWebService/services/HelloWorldService?wsdl
就可以看到应用服务器根据你的代码所生成的wsdl文件了
5、编辑本地客户端测试代码
HelloWorldClient.java
package com.billy.client;
import org.codehaus.xfire.*;
import org.codehaus.xfire.service.*;
import org.codehaus.xfire.service.binding.*;
import org.codehaus.xfire.client.*;
import com.billy.service.IHelloWorldService;
import com.billy.service.IPerson;
import java.net.*;
public class HelloWorldClient {
public static void main(String[] args) {
Service srvcModel = new ObjectServiceFactory().create(IHelloWorldService.class);
XFireProxyFactory factory =new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String helloWorldURL ="http://localhost:8080/TestWebService/services/HelloWorldService";
try{
IHelloWorldService srvc = (IHelloWorldService)factory.create(srvcModel, helloWorldURL);
String result = srvc.example("wangwei");
System.out.println(result);
IPerson p = srvc.getPerson();
System.out.println("name:" + p.getName());
System.out.println("password:" + p.getPassWord());
} catch (MalformedURLException e){
e.printStackTrace();
}
}
}
运行结果:
hello world,wangwei
name:wangwei
password:2210746
tomcat后台日志:
调用example方法:wangwei
调用getPerson方法