此例子转载自:http://www.blogjava.net/WshmAndLily/articles/183417.html
最近的项目中要用到webservices技术,因为之前没有接触过,所以google了一下,发现上面的例子很适合入门,所以就贴了出来,希望对初学者有点帮助,也为自己作个备忘记录。此项目环境为:myeclipse6+tomcat5+XFire1.2, 可以先由myeclipse生成框架代码,然后将下面的代码覆盖项目中的对应文件即可。
我认为下面代码中最应该注意的有两点:
1.如果你要用javaBean作为webservice接口的参数,你所写的javaBean必须有一个默认的无参的构造函数。
2. 为接口中方法的返回类型和传入参数类型配置aegis映射,如下面的IHelloWorldService.aegis.xml 所示(关于为什么要配置 请参阅“Xfire的aegis绑定方式配置小结”)
web.xml如下
<?
xml version="1.0" encoding="UTF-8"
?>
<
web-app
version
="2.4"
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
<
servlet
>
<
servlet-name
>
XFire Servlet
</
servlet-name
>
<
servlet-class
>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</
servlet-class
>
<
load-on-startup
>
0
</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
XFire Servlet
</
servlet-name
>
<
url-pattern
>
/services/*
</
url-pattern
>
</
servlet-mapping
>
<
welcome-file-list
>
<
welcome-file
>
index.jsp
</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
编写Service接口:
package
Test;
import
java.util.List;
public
interface
IHelloWorldService
{ public User HelloWorld(User user); // 传入和返回自定义类型 public List HelloWorldList(); // 返回集合类型 }
编写Service实现类:
package
Test;
import
java.util.ArrayList;
import
java.util.List;
public
class
HelloWorldServiceImpl
implements
IHelloWorldService
{ public User HelloWorld(User user) { if (user.getUsername().equals( " gaoxiang " )) { return new User( " welcome gaoxiang " , " 1234 " ); } else { return new User( " wrong name " , " 1234 " ); } } public List HelloWorldList() { List a = new ArrayList(); User u1 = new User( " 1 " , " 1 " ); User u2 = new User( " 2 " , " 2 " ); User u3 = new User( " 3 " , " 3 " ); a.add(u1); a.add(u2); a.add(u3); return a; } }
为返回类型和传入类型配置aegis映射IHelloWorldService.aegis.xml
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<
mappings
>
<
mapping
>
<
method name
=
"
HelloWorld
"
>
<
parameter index
=
"
0
"
componentType
=
"
Test.User
"
/>
</
method
>
<
method name
=
"
HelloWorld
"
>
<
return
-
type componentType
=
"
Test.User
"
/>
</
method
>
<
method name
=
"
HelloWorldList
"
>
<
return
-
type componentType
=
"
Test.User
"
/>
<!--
定义返回集合类型中元素的type
-->
</
method
>
</
mapping
>
</
mappings
>
在src目录下建立META-INF/xfire目录,并在其编写service.xml
<
beans
>
<!--
第一种方式配置 <service xmlns="http://xfire.codehaus.org/config/1.0"> <name>HelloService</name> <namespace>http://xfire.sttudy/HelloService</namespace> <serviceClass>Test.IHelloWorldService</serviceClass> <implementationClass>Test.HelloWorldServiceImpl</implementationClass> </service>
-->
<!--
使用Spring方式配置
-->
<
bean
name
="HelloService"
class
="org.codehaus.xfire.spring.ServiceBean"
>
<
property
name
="serviceBean"
ref
="echo"
/>
<
property
name
="serviceClass"
value
="Test.IHelloWorldService"
/>
<
property
name
="inHandlers"
>
<
list
>
<
ref
bean
="addressingHandler"
/>
</
list
>
</
property
>
</
bean
>
<
bean
id
="echo"
class
="Test.HelloWorldServiceImpl"
/>
<
bean
id
="addressingHandler"
class
="org.codehaus.xfire.addressing.AddressingInHandler"
/>
</
beans
>
User.java
package
Test;
public
class
User
{ private String username; private String password; public User() { } public User(String username,String password) { this .username = username; this .password = password; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } }
测试WSDL:
http://localhost:8080/XFireStudy/services/IHelloWorldService?wsdl
新建Client工程
使用MyEclipse的自动生成Web Service Client功能(file-new-other-myeclipse-web service-web service client)生成Client代码,需要说明的是,也会生成一个User类,但这个类不是服务端的POJO,而是通过JAXB2绑定的,稍微复杂,读取方式由原来的getUsername()转换成getUsername().getValue();因为getUsername()返回的是一个JAXBElement对象
编写测试代码:
package
client;
import
java.util.List;
import
javax.xml.bind.JAXBElement;
import
javax.xml.namespace.QName;
import
test.IHelloWorldServiceClient;
import
test.IHelloWorldServicePortType;
import
test.User;
public
class
TestWS
{ /** * @param args */ public static void main(String[] args) { IHelloWorldServiceClient client = new IHelloWorldServiceClient(); IHelloWorldServicePortType helloService = client.getIHelloWorldServiceHttpPort(); // 构造user对象稍微复杂些,因为使用了JAXB2绑定,其中QName()参数分别是targetNameSpace和property name User user = new User(); JAXBElement < String > name = new JAXBElement < String > ( new QName(" http://Test " , " username " ),String. class , " gaoxiang " ); JAXBElement < String > password = new JAXBElement < String > ( new QName(" http://Test " , " password " ),String. class , " 1234 " ); user.setUsername(name); user.setPassword(password); System.out.println(helloService.helloWorld(user).getUsername().getValue()); List < User > a = helloService.helloWorldList().getUser(); for (User u1:a) { System.out.println(u1.getUsername().getValue()); } } }