webservice spring xfire的范例
package service;
public interface HelloWorld {
String sayHelloWorld(String name);
}
package service.impl;
import service.HelloWorld;
public class HelloWorldImpl implements HelloWorld {
public String sayHelloWorld(String name) {
String helloWorld = "hello," + name;
return helloWorld;
}
}
src目录下:
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="HelloWorldBean" class="service.impl.HelloWorldImpl"/>
</beans>
client.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="HelloWorldBean" class="service.impl.HelloWorldImpl"/>
</beans>
web.xml:
<display-name>XFireService</display-name>
<!-- begin Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!-- end Spring配置 -->
<!-- begin XFire 配置 -->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
<servlet>
<!-- 配合Spring容器中XFire一起工作的Servlet-->
<servlet-name>xfireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<!-- 在这个URI下开放Web Service服务 -->
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!-- end XFire 配置 -->
和web.xml同目录的xfire-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 定义访问的url-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/HelloWorldService.ws">
<ref bean="HelloWorldService" />
</entry>
</map>
</property>
</bean>
<!-- 使用XFire导出器 -->
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
<!-- 引用xfire.xml中定义的工厂 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用xfire.xml中的xfire实例 -->
<property name="xfire" ref="xfire" />
</bean>
<bean id="HelloWorldService" parent="baseWebService">
<!-- 业务服务bean -->
<property name="serviceBean" ref="HelloWorldBean" />
<!-- 业务服务bean的窄接口类 -->
<property name="serviceClass" value="service.HelloWorld" />
</bean>
public class WebServiceClientTest {
HelloWorld helloWorld = null;
public static void main(String[] args) {//运行这个类,tomcat 必须是启动的
WebServiceClientTest test = new WebServiceClientTest();
test.testClient();
}
public void testClient() { //spring2调用服务
ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
helloWorld = (HelloWorld) ctx.getBean("testWebService");
System.out.println(helloWorld.sayHelloWorld("阿蜜果"));
}
}
public class WebServiceClientTest { //运行这个类,必须启动tomcat
HelloWorld helloWorld = null;
public static void main(String[] args) throws Exception {
WebServiceClientTest test = new WebServiceClientTest();
test.testClient();
}
//通过WSDL文件生成客户端调用程序
public void testClient() throws Exception {
String wsdl = "HelloWorldService.wsdl"; //对应的WSDL文件
Resource resource = new ClassPathResource(wsdl);
Client client = new Client(resource.getInputStream(), null); //根据WSDL创建客户实例
Object[] objArray = new Object[1];
objArray[0] = "阿蜜果";
//调用特定的Web Service方法
Object[] results = client.invoke("sayHelloWorld", objArray);
System.out.println("result: " + results[0]);
}
}
----------------------------------------
//返回对象:
creationComplete="WS.getDataList.send()"
<mx:operation name="getDataList" resultFormat="object">
<mx:DataGrid x="4.5" y="0" dataProvider="{WS.getDataList.lastResult}">
<mx:columns>
<mx:DataGridColumn headerText="用户编号" dataField="id"/>
<mx:DataGridColumn headerText="姓" dataField="lastName"/>
<mx:DataGridColumn headerText="名" dataField="firstName"/>
</mx:columns>
</mx:DataGrid>
public interface HelloWorld {
String sayHelloWorld(String name);
public List<User> getDataList();
}
public class User {
private int id;
private String firstName;//和dataGrid对应
private String lastName;
}