环境
jdk1.5
myeclipse6
jboss4
目标
编写服务类,并用xfire配置一个webservice接口封装这个服务,外部客户端调用webservice接口来访问服务
1 建立web工程webservicetest
2 为工程添加必要库
右击工程引入spring的core库和webservice库,webservice库里选xfire的core和client库
3 编写service和实现
package service;
public interface PojoService {
public String service(String input);
}
package service.impl;
import service.PojoService;
public class PojoServiceImpl implements PojoService {
public String service(String input) {
return "receive: " + input;
}
}
4 编写spring配置文件,提供webservice封装
applicationContext.xml
声明一个普通的service
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="pojoService" class="org.service.impl.PojoServiceImpl"/>
</beans>
applicationContext-webservice.xml
作用
引入xfire的导出器
把service包装成webservice
为webservice设定url
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 使用XFire导出器,它提供封装webservice的框架 -->
<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>
<!-- 封装好的webservice -->
<bean id="myWebService" parent="baseWebService">
<!-- 后台bean -->
<property name="serviceBean" ref="pojoService" />
<!--窄接口,webservice发布的方法根据窄接口控制,可以不把后台bean所有方法公开,即后台bean有方法A和B,而窄接口声明了A,则webservice只公开A方法,B方法不公开 -->
<property name="serviceClass" value="service.PojoService" />
</bean>
<!-- 定义访问的url-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<!-- url和对应的webservice -->
<entry key="/myWebServiceUrl.ws">
<ref bean="myWebService" />
</entry>
</map>
</property>
</bean>
</beans>
注意千万不要用xsd定义,不然会发布失败
<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.0.xsd">
5 配置web.xml,让程序使用spring和xfire
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>XFireService</display-name>
<!-- begin Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/**/applicationContext.xml,/WEB-INF/**/applicationContext-webservice.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>
<init-param>
<!-- 上面的contextConfigLocation只对ContextLoaderListener和ContextLoaderServlet有用,DispatcherServlet要重新定义 -->
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/**/applicationContext.xml,/WEB-INF/**/applicationContext-webservice.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
<!-- 第二种配置方式,任选其一 -->
<servlet>
<servlet-name>xfireServlet</servlet-name>
<servlet-class>
org.codehaus.xfire.spring.XFireSpringServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!-- end XFire 配置 -->
</web-app>
6 部署到jboss,得到wsdl
wsdl是webservice给客户端使用的描述文件,客户端根据这个文件来定位服务
如在web.xml里用第一种配置方式,则访问
http://localhost:8080/webservicetest/myWebServiceUrl.ws?wsdl
如用第二种配置方式,则访问
http://localhost:8080/webservicetest/service
其中service是在web.xml里配好的pattern
把wsdl保存好,两种方式得到的wsdl都可以用的
7 编写testcase,让客户端通过wsdl访问服务
public class WsdlTestCase extends TestCase {
private String path = "./";
private String wsdl = "myWebService.wsdl";
public void testWebService() throws Exception {
Resource resource = new ClassPathResource(path + wsdl);
Client client = new Client(resource.getInputStream(), null);
Object[] objArray = new Object[1];
objArray[0] = "sherwin";
// 调用特定的Web Service方法
Object[] results = client.invoke("service", objArray);
System.out.println("result: " + results[0]);
}
}
把wsdl文件放在java文件同一目录,运行testcase