Exporting beans as web services using XFire

     Once XFire is in your build’s classpath, creating a web service is as simple as following these three steps:
1. Configure an XFireExporter bean to export a Spring bean as a web service.
2. Configure a Spring DispatcherServlet to handle incoming HTTP requests.
3. Configure a handler mapping so as to map DispatcherServlet-handled requests to XFireExporter-exported services.

(1)、定义Interface和Implement
public interface GreetService {
	
	String sayHello(String name);
	
}


import org.springframework.stereotype.Repository;

@Repository("greetService")
public class GreetServiceImpl implements GreetService{

	public String sayHello(String name) {
		return "welcome to you, "+name;
	}

}


import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface HelloService {

	@WebMethod(operationName="hello")
	@WebResult(name="helloWords")
	String sayHello(String name);
	
}

import javax.jws.WebService;

import org.springframework.stereotype.Repository;

@WebService(serviceName="helloService",
		endpointInterface="com.logcd.jws.HelloService")
@Repository("helloService")
public class HelloServiceImpl implements HelloService{

	public String sayHello(String name) {
		return "hello, " + name;
	}

}


(2)、web.xml
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:jwsContext.xml,
			classpath:xfire-servlet.xml
		</param-value>
	</context-param>
	
   <listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
   </listener>
	
	<servlet>
		<servlet-name>xfire</servlet-name>
		<servlet-class>
			<!-- org.springframework.web.servlet.DispatcherServlet  -->
			org.codehaus.xfire.spring.XFireSpringServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>xfire</servlet-name>
		<url-pattern>/jws/*</url-pattern>
	</servlet-mapping>	


(3)、jwsContext.xml
	<context:component-scan base-package="com.logcd.jws"/>

	<!-- import the XFire-defined Spring context 
		where xfire.serviceFactory and xfire are already declared in -->
	<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
	
	<!-- Configuring XFireExporter -->
	<bean id="greetService.xfire"
		class="org.codehaus.xfire.spring.remoting.XFireExporter">
			<property name="serviceFactory"
				ref="xfire.serviceFactory" />
			<property name="xfire" ref="xfire" />
			<property name="serviceBean"
				ref="greetService" />
			<property name="serviceClass"
				value="com.logcd.jws.GreetService" />
	</bean>

	<!-- declaring web services with JSR-181 annotations -->
	<bean id="webAnnotations" 
    	class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/>
	
	<bean id="annotationHandlerMapping"
		class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
			<property name="typeMappingRegistry">
            	<ref bean="xfire.typeMappingRegistry"/>
			</property>
			<property name="xfire" ref="xfire" />
			<property name="webAnnotations">
				<ref bean="webAnnotations"/>
			</property>
	</bean>	


(4)、xfire-servlet.xml(注意命名与web.xml中对应)
	<!-- Mapping requests to XFireExporter -->
	<bean id="handlerMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
			
			<property name="mappings">
				<props>
					<prop key="greetService">greetService.xfire</prop>
					<prop key="helloService">annotationHandlerMapping</prop>
				</props>
			</property>
			
			<!-- 
			<property name="urlMap">
	            <map>
	                <entry key="/">
	                    <ref bean="annotationHandlerMapping"/>
	                </entry>
	            </map>
        	</property>
			 --> 
	</bean>


(5)、Consuming web services
import java.net.MalformedURLException;

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

public class ConsumeService {

	public static void accessJwsUseXFireAPI() throws MalformedURLException{
		Service serviceModel = new ObjectServiceFactory()
	    .create(GreetService.class);
		
		GreetService greetService = 
		(GreetService) new XFireProxyFactory().create(
				serviceModel,
				"http://127.0.0.1/spring_ws/jws/GreetService");
		
		System.out.println(greetService.sayHello("bela"));
	}
	
	public static void accessJwsUseXFireAPI2() throws MalformedURLException{
		Service serviceModel = new AnnotationServiceFactory()
	    .create(HelloService.class);
		
		HelloService helloService = 
		(HelloService) new XFireProxyFactory().create(
				serviceModel,
				"http://127.0.0.1/spring_ws/jws/helloService");
		
		System.out.println(helloService.sayHello("bela"));
	}	
	
	public static void main(String[] args) throws Exception {
		accessJwsUseXFireAPI();
		accessJwsUseXFireAPI2();
	}

}

猜你喜欢

转载自log-cd.iteye.com/blog/831678