MyEclipse中和许多网站的WebService教程以HelloWord为例,内容比较简单,在实际的项目应用中大多都是对象和集合,现在我们扩展下,使用webservice传递对象,字符串,集合;
下面是我们设计的webservice的接口:
1
package
com.test.service;
2
3
import
java.util.List;
4
5
public
interface
IPerson
{
6 //根据用户名获取密码
7 public String getPassword(String name);
8 //根据用户名获取Person对象
9 public Person getPersonByName();
10 //获取全部成员
11 public List<Person> getAllPerson();
12}
其中有三个方法包含了传递:String,Object,List集合;
2,这里我们实现接口中定义的业务逻辑
package
com.test.service;
import
java.util.LinkedList;
import
java.util.List;
public
class
PersonImpl
implements
IPerson
{
public String getPassword(String name) {
return "Password";
}
public Person getPersonByName() {
return new Person("ObjectName","ObjectPass");
}
public java.util.List<Person> getAllPerson(){
List list = new LinkedList();
list.add(new Person("One","PassOne"));
list.add(new Person("Two","PassTwo"));
list.add(new Person("Three","PassThree"));
return list;
}
}
在实际应用中比较复杂,可能用调用持久层的Dao,返回需要的结果,这里我们直接创建一个模拟数据,下面是实现类中用到的对象
1
package
com.test.service;
2
3
public
class
Person
{
4 private String name;
5 private String password;
6
7 public Person() {
8 super();
9 }
10 public Person(String name, String password){
11 super();
12 this.name = name;
13 this.password = password;
14 }
15 public String getPassword(){
16 return password;
17 }
18 public void setPassword(String password){
19 this.password = password;
20 }
21}
22
3,编写XFire要求的WebSevice定义描述文件,如下:
<?
xml version="1.0" encoding="UTF-8"
?>
<
beans
xmlns
="http://xfire.codehaus.org/config/1.0"
>
<
service
>
<
name
>
Person
</
name
>
<
serviceClass
>
com.test.service.IPerson
</
serviceClass
>
<
implementationClass
>
com.test.service.PersonImpl
</
implementationClass
>
<
style
>
wrapped
</
style
>
<
use
>
literal
</
use
>
<
scope
>
application
</
scope
>
</
service
></
beans
>
本文件的放置位置很关键,此文件放在src/META-INF/xfire/services.xml,如果使用MyEclipse新建的不是webapplication,而是webservice项目,这一步就省去自己编写了,编译时会自动编译到classes的相应目录下面。web.z
5,编译xml,定义XfireConfigableServlet
1
<?
xml version="1.0" encoding="UTF-8"
?>
2
<
web-app
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
version
="2.4"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
3
<
servlet
>
4
<
servlet-name
>
XFireServlet
</
servlet-name
>
5
<
servlet-class
>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</
servlet-class
>
6
<
load-on-startup
>
0
</
load-on-startup
>
7
</
servlet
>
8
<
servlet-mapping
>
9
<
servlet-name
>
XFireServlet
</
servlet-name
>
10
<
url-pattern
>
/services/*
</
url-pattern
>
11
</
servlet-mapping
>
12
<
welcome-file-list
>
13
<
welcome-file
>
index.jsp
</
welcome-file
>
14
</
welcome-file-list
>
15
</
web-app
>
16
17
所有的工作都已经作完,接下来在部署在tomcate下,启动服务器,在地址栏键入:http://127.0.0.1:8080/MyWebService/srevice/Person?wsdl
如果出现xml描述文件就成功了;