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
	
		contextConfigLocation
		
			classpath:jwsContext.xml,
			classpath:xfire-servlet.xml
		
	
	
   
       
           org.springframework.web.context.ContextLoaderListener
       
   
	
	
		xfire
		
			
			org.codehaus.xfire.spring.XFireSpringServlet
		
		1
	
	
	
		xfire
		/jws/*
		


(3)、jwsContext.xml
	

	
	
	
	
	
			
			
			
			
	

	
	
	
	
			
            	
			
			
			
				
			
		


(4)、xfire-servlet.xml(注意命名与web.xml中对应)
	
	
			
			
				
					greetService.xfire
					annotationHandlerMapping
				
			
			
			 
	


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

}
  • dependentPackage.rar (4.3 MB)
  • 下载次数: 0

你可能感兴趣的:(Exporting beans as web services using XFire)