wsdl文档结构图

以下是基于cxf 2.13实现的webservice

接口类:

View Code
package com.cxf213;


import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/** 
 * @Package com.cxf213

 * @ClassName: HelloWorld

 * @Description: TODO(这里用一句话描述这个类的作用)

 * @author andy

 * @date 2013-1-28 下午11:12:52

 */
@WebService
public interface HelloWorld {
    @WebResult(name="sayHelloResult")String sayHello(@WebParam(name="name")String name);
}

实现类:

View Code
package com.cxf213;


import javax.jws.WebService;

/** 
 * @Package com.cxf213

 * @ClassName: HelloWorldImpl

 * @Description: TODO(这里用一句话描述这个类的作用)

 * @author andy

 * @date 2013-1-28 下午11:16:57

 */
@WebService
public class HelloWorldImpl implements HelloWorld {

    /** (non Javadoc)
    
     * <p>Title: sayHello</p>
    
     * <p>Description: </p>
    
     * @param name
     * @return
    
     * @see com.cxf213.HelloWorld#sayHello(java.lang.String)
     */
    public String sayHello(String name) {
        // TODO Auto-generated method stub
        System.out.println("cxf 2.13 webservice");
        return "hello:" + name;
    }

}

服务器端:

View Code
package com.cxf213;


import org.apache.cxf.endpoint.Server;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

/** 
 * @Package com.cxf213

 * @ClassName: MainServer

 * @Description: TODO(这里用一句话描述这个类的作用)

 * @author andy

 * @date 2013-1-28 下午11:18:28

 */
public class MainServer {
    public static void main(String[] args) {
            JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
            //设置webservice的具体实现类
            factory.setServiceClass(HelloWorldImpl.class);
            //设置webservice的地址
            factory.setAddress("http://localhost:8080/HelloWorld");
            
            //添加一个输入的intercepter
            factory.getInInterceptors().add(new LoggingInInterceptor());
            
            //添加一个输出的intercepter
            factory.getOutInterceptors().add(new LoggingOutInterceptor());
            //创建一个webservice
            Server server = factory.create();
            server.start();
    }
}

运行服务器端main方法之后访问:http://localhost:8080/HelloWorld?wsdl

就会有一个xml文档结构图:

View Code
<!--文档缩写
<wsdl:definitions name="HelloWorldImplService" targetNamespace="http://cxf213.com/">
    <wsdl:types></wsdl:types>
    <wsdl:message name="sayHello"></wsdl:message> 
    <wsdl:message name="sayHelloResponse"></wsdl:message>
    <wsdl:portType name="HelloWorld"></wsdl:portType>
    <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld"></wsdl:binding>
    <wsdl:service name="HelloWorldImplService"></wsdl:service>
</wsdl:definitions>
-->

<?xml version='1.0' encoding='UTF-8'?>
    <!-- name 为实现类+service  targetNamespace为包名倒过来-->
<wsdl:definitions name="HelloWorldImplService"
    targetNamespace="http://cxf213.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://cxf213.com/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <!-- 参数类型:包括输入参数,和返回值参数 -->
    <wsdl:types>
        <xs:schema attributeFormDefault="unqualified"
            elementFormDefault="unqualified" targetNamespace="http://cxf213.com/"
            xmlns:tns="http://cxf213.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">

            <!-- 输入参数类型 (总计一样的把它定成一个类型) -->
            <xs:element name="sayHello" type="tns:sayHello" />

            <!-- 返回值类型  (总计一样的把它定成一个类型)-->
            <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />

            <!-- 输入参数的详细情况 -->
            <xs:complexType name="sayHello">
                <xs:sequence>
                    <!-- name为参数的具体名字,type为参数的类型 -->
                    <xs:element minOccurs="0" name="name" type="xs:string" />
                </xs:sequence>
            </xs:complexType>

            <!-- 返回值类型的详细情况 -->
            <xs:complexType name="sayHelloResponse">
                <xs:sequence>
                    <!-- name为参数的具体名字,type为参数的类型 -->
                    <xs:element minOccurs="0" name="sayHelloResult" type="xs:string" />
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
    </wsdl:types>

    <!-- 把请求包装成一个message -->
    <wsdl:message name="sayHello">
        <wsdl:part element="tns:sayHello" name="parameters">
        </wsdl:part>
    </wsdl:message>

    <!-- 把输出响应包装成一个message -->
    <wsdl:message name="sayHelloResponse">
        <wsdl:part element="tns:sayHelloResponse" name="parameters">
        </wsdl:part>
    </wsdl:message>

    <!-- 对应我们的接口 -->
    <wsdl:portType name="HelloWorld">
        <!-- 接口里面暴露出来的方法 -->
        <wsdl:operation name="sayHello">
            <!-- 这里面的message参数对应上面的message,下同 -->
            <wsdl:input message="tns:sayHello" name="sayHello">
            </wsdl:input>
            <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
            </wsdl:output>
        </wsdl:operation>
    </wsdl:portType>

    <!-- 把定义的接口绑定 ,type是需要绑定的接口-->
    <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="sayHello">
            <soap:operation soapAction="" style="document" />
            <wsdl:input name="sayHello">
                 <!-- 输入参数为文本 -->
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output name="sayHelloResponse">
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>


   <!-- 发布成webservice -->
    <wsdl:service name="HelloWorldImplService">
        <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
           <!-- 发布成的地址 -->
            <soap:address location="http://localhost:8080/HelloWorld" />
        </wsdl:port>
    </wsdl:service>
    
    
</wsdl:definitions>

 

 

客户端:

View Code
package com.cxf213;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

/** 
 * @Package com.cxf213

 * @ClassName: HelloWorldClient

 * @Description: TODO(这里用一句话描述这个类的作用)

 * @author andy

 * @date 2013-1-29 上午10:50:16

 */
public class HelloWorldClient {
    public static void main(String[] args) {
        
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setAddress("http://localhost:8080/HelloWorld");
        factoryBean.setServiceClass(HelloWorld.class);
        HelloWorld helloWorld = (HelloWorld)factoryBean.create();
        System.out.println(helloWorld.sayHello("张三"));
    }
}

 

 

 

 

 

posted on 2013-01-29 23:46  lovebeauty  阅读(608)  评论(0编辑  收藏  举报

导航