XFire与Spring结合

1、使用org.codehaus.xfire.spring.XFireSpringServlet与ServiceBean

1.1 web.xml的配置

 


 Spring Image Database
 Spring Image Database sample application
 
    
       contextConfigLocation
       
     classpath:applicationContext-webservice.xml
    
    
 
 
  org.springframework.web.util.Log4jConfigListener
 
 
 
        org.springframework.web.context.ContextLoaderListener
 

 
        XFireServlet
        XFire Servlet
        org.codehaus.xfire.spring.XFireSpringServlet
    
               
    
        XFireServlet
        /services/*
    

 
  index.jsp
 
 


 

1.2 applicationContext-webservice.xml的配置:

 



    
    
    
        
        
        
            
                
            
        
    

    

    
 
   
        
        
    

    



 

1.3 这样将会发布两个service,BookServiceEchoService。随后就可以使用client端进行测试了。

     //测试BookService

 

 public static void main(String args[])
    { 
        String serviceURL = "http://127.0.0.1:9001/xfire/services/BookService";
        Service serviceModel = new ObjectServiceFactory().create(BookService.class,null,"http://xfire.codehaus.org/BookService",null);
        XFireProxyFactory serviceFactory = new XFireProxyFactory();
        try
        {
            BookService service = (BookService) serviceFactory.create(serviceModel, serviceURL);
            Client client = Client.getInstance(service);
            client.addOutHandler(new OutHeaderHandler());
            Book[] books = service.getBooks();
            System.out.println("BOOKS:");
            for (int i = 0; i < books.length; i++)
            {
                System.out.println(books[i].getTitle());
            }
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
    }

 
1.4 忘了BookService及其实现了。

 

 public interface BookService
    {
              public Book[] getBooks();
    
              public Book findBook(String isbn);
    
             public Map getBooksMap();
   }

 

    public class BookServiceImpl implements BookService
    {
    private Book onlyBook;
    
    public BookServiceImpl()
    {
        onlyBook = new Book();
        onlyBook.setAuthor("Dan Diephouse");
        onlyBook.setTitle("Using XFire");
        onlyBook.setIsbn("0123456789");
     }

     public Book[] getBooks() 
     {
        return new Book[] { onlyBook };
     }
    
     public Book findBook(String isbn)
     {
        if (isbn.equals(onlyBook.getIsbn()))
            return onlyBook;
        
        return null;
     }

     public Map getBooksMap() {
  Map result = new HashMap();
  result.put(onlyBook.getIsbn(), onlyBook);
  return result;
     }
    }

 

1.5 简单的测试就是通过IE,输入http://ip:port/context/services/BookService?wsdl或者http://ip:port/context/services/EchoService?wsdl,将会出现相应的wsdl文档。

     如果只是输入http://ip:port/context/services/BookService,会出现Invalid SOAP request.这也说明配置正确。

2、直接集成Spring(通过Spring的org.springframework.web.servlet.DispatcherServlet)

2.1 web.xml配置



    
        contextConfigLocation
        
        classpath:org/codehaus/xfire/spring/xfire.xml
    

    
        log4jConfigLocation
        /WEB-INF/log4j.properties
    

    
        org.springframework.web.util.Log4jConfigListener
    

    
        org.springframework.web.context.ContextLoaderListener
    

    
        xfire
        org.springframework.web.servlet.DispatcherServlet
    

    
        xfire
        /*
    



 
2.2 xfire-servlet.xml配置


    
    
        
            
                
                    
                
            
        
    

    

    
    
        
            
        
        
            
        
        
            
        
        
            org.codehaus.xfire.spring.example.Echo
        
    
    

 
2.3 余下的配置跟第一种方法一样。

你可能感兴趣的:(WebServices)