使用XFire+Spring+Struts构建Web Service及测试
FROM: http://shade8109.blog.163.com/blog/static/130110604200911294257292/?fromdm&fromSearch&isFromSearchEngine=yes
1. 开发环境:
JDK 1.5, Tomcat 5.5.25, MyEclipse 6.0 其实也没什么特别之处.
2. 建立工程, 配好环境:
在MyEclipse下建立Web Project,取名为:wss, (意为:Webservice, Spring, Struts)
对wss增加:Web Service Capabilities, 记得选择XFire的Core Libraries
对wss增加:Struts Capabilities
3.修改web.xml,使之响应不同的请求以及XFire与Spring的结合
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>YMCN-XFireService</display-name>
<!-- begin Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.ymcn.filter.CharacterEncoding</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>
4.在/WEB-INF/下编写applicationContext.xml 和 xfire-servlet.xml两文件,此二文件是必须的,前一个是Spring的,后一个是XFire的,其为被XFire自动加载.
applicationContext.xml 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>
5.xfire-servlet.xml 的内容:
<?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导出器 -->
<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>
<!-- 定义所有访问 WEB服务的url -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/HelloWorldService.ws">
<ref local="HelloWorldService"/>
</entry>
</map>
</property>
</bean>
<!-- HelloWorld WEB服务 -->
<bean id="HelloWorld" class="org.ymcn.ws.impl.HelloWorldImpl"/>
<bean id="HelloWorldService" parent="baseWebService">
<!-- 业务服务bean -->
<property name="serviceBean" ref="HelloWorld"/>
<!-- 业务服务bean的窄接口类 -->
<property name="serviceClass" value="org.ymcn.ws.HelloWorld"/>
</bean>
</beans>
此时,我们就已经构建出了一个Web Service,我们在IE中输入:http://localhost:8888/wss/HelloWorldService.ws?wsdl
就能看到WSDL.
6.通过Java application 测试:编写JAVA简单测试类:
首先,在wss/src下建个客户端调用Web Service的Spring配置文件:client.xml,内容为:
<?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="HelloWorldService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
<property name="serviceClass"><value>org.ymcn.ws.HelloWorld</value></property>
<property name="wsdlDocumentUrl"><value>http://localhost:8888/wss/HelloWorldService.ws?wsdl</value></property>
</bean>
</beans>
到此我们可以通过client.xml获得HelloWorld
package org.ymcn.test.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.ymcn.ws.HelloWorld;
public class HelloWorld2 {
HelloWorld helloWorld = null;
public static void main(String[] args) {
HelloWorld2 test = new HelloWorld2();
test.testClient();
}
public void testClient() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
helloWorld = (HelloWorld)ctx.getBean("HelloWorldService");
System.out.println(helloWorld.sayHello("老牛啊"));
}
}
运行此程序,如输出:你好啊,老牛啊 说明正确.
7.在表示层(Struts)通过JSP调用Web Service:
JSP文件:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="html" uri="html" %>
<%@ taglib prefix="bean" uri="bean" %>
<%@ taglib prefix="logic" uri="logic" %>
<html>
<head>
<title>测试WebService - HelloWorld</title>
</head>
<body>
<logic:present name="NAME" scope="request">
<font color="red"><bean:write name="NAME" scope="request"/></font>
</logic:present>
<br>
<form action="/wss/ws/HelloWorld.ymcn" method="post">
用户名:<input type="text" name="name" style="width:500px">
<br><br>
<input type="submit" name="Submit" value=" 提交 "/>
</form>
</body>
</html>
8,struts-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/ws/HelloWorld" type="org.ymcn.struts.action.WSHelloWorldAction" scope="request" validate="false">
<forward name="hello-ok" path="/ws/helloWorld.jsp"/>
</action>
</action-mappings>
<message-resources parameter="org.ymcn.struts.i18n.i18n" />
</struts-config>
9.JSP请求Action类:WSHelloWorld
package org.ymcn.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.ymcn.ws.HelloWorld;
public class WSHelloWorldAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = (String)request.getParameter("name");
ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
HelloWorld helloWorld = (HelloWorld)ctx.getBean("HelloWorldService");
String result = helloWorld.sayHello(name);
if(result == null) {
result = "对不起, 调用WEB服务失败, 请重试......";
}
request.setAttribute("NAME", result);
return mapping.findForward("hello-ok");
}
}
到些,所有的工作已完成,部署WEB工程,启动Tomcat
10.在IE中输入:http://localhost:8888/wss/ws/helloWorld.jsp
点击提交:
恭喜你,你的工作得到了回报~~~~~~~~~~~~~~~~~~~~~~~~~~
最后是过滤器类了,我用的全是UTF-8,内容如下:
package org.ymcn.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharacterEncoding implements Filter
{
protected String encoding;
protected FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
this.encoding = "utf-8";
}
public void destroy( )
{
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
protected String selectEncoding(ServletRequest request)
{
return (this.encoding);
}
}
好的,所有工作做完了