zoukankan      html  css  js  c++  java
  • 使用XFire+Spring+Struts构建Web Service及测试

     Web Service已经搞了很久了,也很久没有搞了,刚好把手头的事做完,顺便再看看.

          以前都是用的MyEclipse自带的XFire做的,参照它的帮助文档,也算是入个门.但是测试没有做过.

          这次也是大大利用XFire的强大功能,以及它和Spring的完美结合和Spring框架构建Web Service,然后利用Struts做客户端测试,思想是比较清楚的.

          还是以HelloWorld这个JAVA经典作为讲解:

         一. 开发环境:
            JDK 1.5,   Tomcat 5.5.25,   MyEclipse 6.0 其实也没什么特别之处.

         二. 建立工程, 配好环境:
            在MyEclipse下建立Web Project,取名为:wss, (意为:Webservice, Spring, Struts)

            对wss增加:Web Service Capabilities, 记得选择XFire的Core Libraries

            对wss增加:Struts Capabilities

          三.修改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>/*</url-pattern>
    </filter-mapping>

    <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>
    <!-- 配合Spring容器中XFire一起工作的Servlet-->
    <servlet>
        <servlet-name>xfireServlet</servlet-name>
        <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
    </servlet>

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
          <param-name>debug</param-name>
          <param-value>3</param-value>
        </init-param>
        <init-param>
          <param-name>detail</param-name>
          <param-value>3</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>xfire</servlet-name>
        <url-pattern>*.ws</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>xfireServlet</servlet-name>
        <!-- 在这个URI下开放Web Service服务 -->
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>
    <!-- end XFire 配置 -->

    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.ymcn</url-pattern>
    </servlet-mapping>

    <taglib>
        <taglib-uri>html</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>logic</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>bean</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>nested</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
    </taglib>

    </web-app>

    四.编写接口类:HelloWorld

    package org.ymcn.ws;

    public interface HelloWorld {
        String sayHello(String name);
    }

    五.编写实现类:HelloWorldImpl

    package org.ymcn.ws.impl;

    import org.ymcn.ws.HelloWorld;

    public class HelloWorldImpl implements HelloWorld {

    public String sayHello(String name) {
       if(name == null) {
        return null;
       }
      
       return "你好, " + name;
    }
    }

    /************* 注意包路径,在后面我将把工程图贴上,以免错误 ***********/

    六.在/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>

    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.

    七.通过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("老牛啊"));
        }
    }


    运行此程序,如输出:你好啊,老牛啊 说明正确.

    八.在表示层(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="500px">
    <br><br>
    <input type="submit" name="Submit" value=" 提交 "/>
    </form>

    </body>
    </html>

    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>

    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");
       /* 调用WebService, 获得值返回 */
       ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
         HelloWorld helloWorld = (HelloWorld)ctx.getBean("HelloWorldService");
         String result = helloWorld.sayHello(name);
        
       /* 保存在request中, 返回 */
         if(result == null) {
         result = "对不起, 调用WEB服务失败, 请重试......";
         }
        
         request.setAttribute("NAME", result);
        
       return mapping.findForward("hello-ok");
    }
    }

    到些,所有的工作已完成,部署WEB工程,启动Tomcat

    在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);
        }
    }

    好的,所有工作做完了

    (http://hi.baidu.com/obullxl/blog/item/00687e59b15706292834f0a5.html)

  • 相关阅读:
    intellij idea 为JavaEE项目建立Servlet
    DNS无响应
    取消Gridvie中button的焦点
    android.os.NetworkOnMainThreadException
    java.net.MalformedURLException: Protocol not found:
    Failure [INSTALL_FAILED_OLDER_SDK]
    android library projects cannot be launched
    CDQ分治的嵌套
    CDQ分治入门
    最远 Manhattan 距离
  • 原文地址:https://www.cnblogs.com/cuker919/p/4878630.html
Copyright © 2011-2022 走看看