CXF简单例子

 service框架用apache CXF,服务器tomcat ,还有spring框架
        OpenSource中关于CXF的简介:Apache CXF一个开源的Service框架,它实现了JCP与Web Service中一些重要标准。CXF简化了构造,集成,面向服务架构(SOA)业务组件与技术的灵活复用。在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式(或binding)和网络协议(transports)包括SOAP、XML(通过HTTP或JMS)进行访问。CXF同样支持多种model 如:JAX-WS,JBI,SCA和CORBA service。CXF设计成可灵活部署到各种容器中包括Spring-based,JBI,SCA, Servlet和J2EE容器。

       用CXF构建webservice程序的大致流程如下:

       1 配置web.xml文件,加载CXF

       2 编写java接口,和接口的实现

       3 在spring的applicationContext.xml中配置java文件

       4 创建beans.xml文件,里面配置接口的实现类

      5 运行tomca服务器即可生成wsdl文件

      6 编写测试代码:编写java测试文件和client-beans.xml文件

      7编写运行客户端文件所需的服务器,可以单独编写,也可以在随tomcat启动

示例代码。首先下载cxf,然后解压,将lib下的所有jar包拷到工程下面

    一 最简单的helloword

     1  web.xml


    
        contextConfigLocation
        WEB-INF/beans.xml
    

    
        
            org.springframework.web.context.ContextLoaderListener
        
    

    
        CXFServlet
        CXF Servlet
        
            org.apache.cxf.transport.servlet.CXFServlet
        
        1
    

    
        CXFServlet
        /*
    


 2  接口HelloWord.java和接口的实现HelloWordImpl.java

package com.demo;

import javax.jws.WebService;

/** *//**
* service interface
* 
* @author
* 
*/
@WebService
public interface HelloWord ...{
    String sayHi(String text);
}




package com.demo;

import javax.jws.WebService;

/** *//**
* implementation
* 
* @author 
* 
*/
@WebService
public class HelloWordImpl implements HelloWord ...{

    public String sayHi(String text) ...{
        return "Hi" + text;
    }
}

 

3 beans.xml





    
    
    

    


 

 

4  启动tomcat服务器,在浏览器里面输入http://localhost:8888/helloword后页面上会出现

{http://demo.com/}HelloWordImplPort ,点击会看到生成的wsdl文件http://localhost:8888/helloword/HelloWord?wsdl



5 客户端Client.java和client-beans.xml

package com.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class Client ...{

    private Client() ...{

    }

    public static void main(String args[]) throws Exception ...{
        // START SNIPPET: client
        ApplicationContext  context = new ClassPathXmlApplicationContext(
                new String[] ...{ "com/demo/client-beans.xml" });

        HelloWord client = (HelloWord) context.getBean("client");

        String response = client.sayHi("+what????");
        System.out.println("Response: " + response);
        System.exit(0);
        // END SNIPPET: client
    }
}




    

    
        
        
    


  

6 客户端运行时所需的服务器Server.java

package com.demo;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class Server ...{

    protected Server() throws Exception ...{
        System.out.println("Starting Server");

        org.mortbay.jetty.Server server = new org.mortbay.jetty.Server();

        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(9002);
        server.setConnectors(new Connector[] ...{connector});

        WebAppContext webappcontext = new WebAppContext();
        webappcontext.setContextPath("/");

        webappcontext.setWar("WebRoot");

        HandlerCollection handlers = new HandlerCollection();
        handlers.setHandlers(new Handler[] ...{webappcontext, new DefaultHandler()});

        server.setHandler(handlers);
        server.start();
        System.out.println("Server ready...");
        server.join();
    }

    public static void main(String args[]) throws Exception ...{
        new Server();
    }

}

 

7 运行客户端Client.java的顺序,先启动tomcat,然后运行Server.java,最后运行Client.java


二 不单独编写Server.java文件,让其和tomcat一同启动.需要改动的地方

1 web.xml


        contextConfigLocation
        
           WEB-INF/applicationContext.xml
           WEB-INF/beans.xml
        

    


    
        
            org.springframework.web.context.ContextLoaderListener
        

    


    
        CXFServlet
        CXF Servlet
        
            org.apache.cxf.transport.servlet.CXFServlet
        

        2
    


    
        CXFServlet
        /webservice/*
    

    


2 applicationContext.xml 配置文件中com.demo.HelloWordFind,  HelloWordFind.java可以写一个查询表HELLO_WORD中所有内容的代码。相应的HelloWord.java和HelloWordImpl.java也需要修改





  
        
            
                WEB-INF/jdbc.properties
            

        

    

    
        
        
        
        
    

    
    
        
        
            
                mapping/HelloWord.hbm.xml
            

        

        
            
                org.hibernate.dialect.DB2Dialect
                true
            

        

        
            
                
                    
                

            

        

    


    
        
        
    





3 beans.xml



    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    
    
    
    
      
      
          
    

    



4 主要需要改动的已经完成。客户端代码变化不大。

你可能感兴趣的:(WebServices,Servlet,Bean,Spring,WebService,XML)