用cxf编写webservice

cxf的官网文档地址:http://cxf.apache.org/docs/
1.建立一个web project,名称为cfx-new,部署目录写成ROOT
2.下载最新的cxf包http://cxf.apache.org/download.html,我下载的是apache-cxf-2.5.2.zip,直接将lib下的所有jar拷贝到lib下
3.新建一个接口类HelloWorld

package info.frady.service;  
  
import javax.jws.WebParam;  
import javax.jws.WebService;  
  
@WebService  
public interface HelloWorld {  
    String sayHi(String text);  
} 

 4.建立一个接口的实现类HelloWorldImpl

    package info.frady.service.impl;  
      
    import info.frady.service.HelloWorld;  
      
    import javax.jws.WebService;  
      
    @WebService(endpointInterface = "info.frady.service.HelloWorld",   
                serviceName = "helloWorld")  
    public class HelloWorldImpl implements HelloWorld {  
       
        public String sayHi(String text) {  
            return "Hello " + text;  
        }  
    }  

 说明下:
*@WebService:申明为webservice的注解,这样cxf就知道这是个webservice了
*endpointInterface:要暴露的接口类
 *serviceName :    服务名,其实就是调用地址
5.在WEB-INF目录下新建beans.xml,如下:

 

    <?xml version="1.0" encoding="UTF-8"?>  
    <!-- START SNIPPET: beans -->  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        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">  
      
        <import resource="classpath:META-INF/cxf/cxf.xml" />  
        <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
        <jaxws:endpoint   
          id="helloWorld"   
          implementor="info.frady.service.impl.HelloWorldImpl"   
          address="/helloWorld" />  
    </beans>  

 说明:
注: implementor :接口类的实现类
        address:   要和注释里面声明的服务名对应,对应的是外部访问地址
6.在修改web.xml文件,如下

    <?xml version="1.0" encoding="ISO-8859-1"?>  
      
    <!DOCTYPE web-app  
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
        "http://java.sun.com/dtd/web-app_2_3.dtd">  
      
    <!-- START SNIPPET: webxml -->  
    <web-app>  
        <context-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/beans.xml</param-value>  
        </context-param>  
      
        <listener>  
            <listener-class>  
                org.springframework.web.context.ContextLoaderListener  
            </listener-class>  
        </listener>  
      
        <servlet>  
            <servlet-name>CXFServlet</servlet-name>  
            <display-name>CXF Servlet</display-name>  
            <servlet-class>  
                org.apache.cxf.transport.servlet.CXFServlet  
            </servlet-class>  
            <load-on-startup>1</load-on-startup>  
        </servlet>  
      
        <servlet-mapping>  
            <servlet-name>CXFServlet</servlet-name>  
            <url-pattern>/services/*</url-pattern>  
        </servlet-mapping>  
    </web-app>  
    <!-- END SNIPPET: webxml -->  

到此代码部分结束,所需jar包列表


用cxf编写webservice

 

7.启动tomcat
8.输入地址http://localhost:9081/services/,出现WSDL : {http://impl.service.frady.info/}HelloWorld说明服务端成功。
9.编写测试代码

    import info.frady.domain.Person;  
    import info.frady.service.HelloWorld;  
      
    import org.apache.cxf.interceptor.LoggingInInterceptor;  
    import org.apache.cxf.interceptor.LoggingOutInterceptor;  
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
      
      
    public class TestHello {  
      
        /** 
         * @param args 
         */  
        public static void main(String[] args) {  
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
            factory.getInInterceptors().add(new LoggingInInterceptor());  
            factory.getOutInterceptors().add(new LoggingOutInterceptor());  
            factory.setServiceClass(HelloWorld.class);  
            factory.setAddress("http://localhost:8080/services/HelloWorld");  
            HelloWorld client = (HelloWorld) factory.create();  
            String reply = client.sayHi("frady");  
            System.out.println("Server said: " + reply);      
            System.exit(0);   
        }  
      
    }  

 

 

你可能感兴趣的:(CXF,WebServie)