Posted on 2008-03-03 11:27
semovy 阅读(2700)
评论(0) 编辑 收藏 所属分类:
WebService
工程结构图
首先建立一个web工程作为Service端,在classpath中加载XFire Jar包,并编写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());
}
}
}