xfireとspringを使用したwebserviceアプリケーションの構築

9796 ワード

1、新しいインタフェースと実装クラス
package org.springframework.mvc.demo.ws;

public interface HelloWorld {
     String sayHi(String text);
 }

package org.springframework.mvc.demo.ws;

public class HelloWorldImpl implements HelloWorld {

	public String sayHi(String text) {

		return "Hello " + text;
	}		
}

 2、web.xmlの構成
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

   <context-param>   
        <param-name>log4jConfigLocation</param-name>   
        <param-value>/WEB-INF/classes/log4j.properties</param-value>   
    </context-param>  


	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/spring/root-context.xml
			classpath:org/codehaus/xfire/spring/xfire.xml
		</param-value>
	</context-param>
	<!-- Reads request input using UTF-8 encoding -->
	<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	  <listener>   
        <listener-class>   
            org.springframework.web.context.ContextLoaderListener   
        </listener-class>   
    </listener>  
	<!-- Handles all requests into the application -->
	<servlet>
		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value> 
				/WEB-INF/spring/appServlet/servlet-context.xml
				/WEB-INF/xfire-servlet.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet>
		<!--   Spring   XFire     Servlet -->
		<servlet-name>xfireServlet</servlet-name>
		<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>xfireServlet</servlet-name>
		<!--    URI   Web Service   -->
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>
	<!-- end XFire    -->
</web-app>

 3、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" /> -->
    <!--     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="org.springframework.mvc.demo.ws.HelloWorld" />
    </bean>
</beans>

4、servlet-context.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<!-- Scans the classpath of this application for @Components to deploy as beans -->
	<context:component-scan base-package="org.springframework.samples.mvc" />

	<!-- Configures the @Controller programming model -->
	<mvc:annotation-driven />

	<!-- Forwards requests to the "/" resource to the "welcome" view -->
	<mvc:view-controller path="/" view-name="welcome"/>

	<!-- Configures Handler Interceptors -->	
	<mvc:interceptors>
		<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
	</mvc:interceptors>

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory 
	<mvc:resources mapping="/resources/**" location="/resources/" />-->

	<!-- Saves a locale change using a cookie 
	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
-->
	<!-- Application Message Bundle
	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basename" value="/WEB-INF/messages/messages" />
		<property name="cacheSeconds" value="0" />
	</bean> -->

	<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	<bean id="HelloWorldBean" class=" org.springframework.mvc.demo.ws.HelloWorldImpl"></bean>

</beans>

  
5、正常に発行されたかどうかをテストする
http://localhost:8080/lsdemo/HelloWorldService.ws?wsdl
6、webserviceを呼び出す
package org.springframework.mvc.demo.ws;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

public class WebServiceApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Service s=new ObjectServiceFactory().create(HelloWorld.class);
		XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
		String url="http://localhost:8080/lsdemo/service/HelloWorld";
		
		        try
		        {            
		        	HelloWorld hs=(HelloWorld) xf.create(s,url);
		            String st=hs.sayHi("zhangjin");
		            System.out.print(st);
		        }
		        catch(Exception e)
		        {
		            e.printStackTrace();
		        }
		    }

	}