`

CXF的第一个例子 (附 wsdl详解)

    博客分类:
  • SOA
阅读更多

1、在eclipse中 创建一个java project。


2、把cxf的lib包中的jar文件放入项目的类路径。


3、4个文件如下:(cxf内置了jetty server, 省去了很多发布的麻烦)


HelloWorld.java

package test;
import javax.jws.WebService;
@WebService
public interface HelloWorld {    
    String sayHello(String str);
}

HelloWorldImpl.java

package test;
import javax.jws.WebService;
@WebService
public class HelloWorldImpl implements HelloWorld {
    public String sayHello(String str) {        
        System.out.println(" sayHello is called.");        
        return "Hello " + str +" !";
    }
}

MainServer.java

package test;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class MainServer {
    public static void main(String[] args) {        
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        factory.setServiceClass(HelloWorldImpl.class);
        factory.setAddress("http://localhost:8080/HelloWorld");        
        Server server = factory.create();
        server.start();
    }
}

HelloWorldClient.java

package test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class HelloWorldClient {
    public static void main(String[] args) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress("http://localhost:8080/HelloWorld");
        factory.setServiceClass(HelloWorld.class);
        HelloWorld helloworld = (HelloWorld) factory.create();
        System.out.println(helloworld.sayHello("CY"));
    }
}


4、先运行MainServer.java,(即启动服务器,发布server);

再运行HelloWorldClient.java,可以看到结果。

通过访问 http://localhost:8080/HelloWorld?wsdl  可以看web service 是否发布成功。


================ WSDL 文件 剖析 =========================

 

// 这里的name 是 发布的service类名 + “Service”, targetNamespace 是取决于 发布类所在的包

<wsdl:definitions name="HelloWorldImplService" targetNamespace="http://test/">
  // types 的作用是 定义 输入输出参数 都是什么样子的(类型)
  <wsdl:types >
    <xs:schema elementFormDefault="unqualified" targetNamespace="http://test/" version="1.0">

         // 输入参数名字为‘sayHello’,类型是复杂类型‘sayHello’,在下面定义
      <xs:element name="sayHello" type="tns:sayHello"/>

         // 输出参数名字为‘sayHelloResponse’,类型是复杂类型sayHelloResponse, 在下面定义
      <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>

          // 输入参数类型的具体定义:包含一个element, 名字为arg0,类型为string
      <xs:complexType name="sayHello">
        <xs:sequence>

               // 这里的name 是自动生成的。当然,也可以在代码中指定名字。

               // public @WebResult(name="sayHelloResult")String sayHello(@WebParam(name="name")String str)
          <xs:element minOccurs="0" name="arg0" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="sayHelloResponse">
        <xs:sequence>
          <xs:element minOccurs="0" name="return" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
  </wsdl:types>
  // 这个message代表 输入信息。这个输入信息的类型是sayHello,在<types>中定义过
  <wsdl:message name="sayHello">
    <wsdl:part element="tns:sayHello" name="parameters">
    </wsdl:part>
  </wsdl:message>

   // 这个message代表 输出信息。这个输出信息的类型是sayHelloResponse,在<types>中定义过
  <wsdl:message name="sayHelloResponse">
    <wsdl:part element="tns:sayHelloResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  // portType 就是我们定义的接口。一个接口对应一个port
  <wsdl:portType name="HelloWorld">

       // 这里的一个operation就是 接口中的一个方法
    <wsdl:operation name="sayHello">
    <wsdl:input message="tns:sayHello" name="sayHello">
      </wsdl:input>
    <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
      </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  // 把接口进行 soap 绑盯。
  <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">

      // 这里指明 绑盯的协议为 http,style为document
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

       //  具体方法的绑盯类型定义
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="sayHello">

             // literal 文本
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  // 把n个接口放到一起,总称为一个service
  <wsdl:service name="HelloWorldImplService">

      // 这里一个port就是一个接口。对应的绑盯刚刚定义过
    <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">

          // 这个接口的地址
      <soap:address location="http://localhost:8080/HelloWorld"/>
    </wsdl:port>
  </wsdl:service>
 
</wsdl:definitions>

分享到:
评论
1 楼 waltertan1988 2010-03-27  
我的写的例子和楼主的基本一样,而且程序也能正常执行,但生成的wsdl却比你少了很多啊。(myeclipse8+cxf2.2.7)
<wsdl:definitions name="HelloWorldImplService" targetNamespace="http://impl.services/">
<wsdl:import location="http://localhost:8080/HelloWorld?wsdl=HelloWorld.wsdl" namespace="http://services/">
    </wsdl:import>
−
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="ns1: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>
−
<wsdl:service name="HelloWorldImplService">
−
<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8080/HelloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

请问是什么缘故吗?

相关推荐

    cxf + spring 例子

    CXF允许开发者选择使用Java语言定义服务接口,或者使用WSDL第一方式,即从WSDL文件生成Java代码。 2. **Spring框架**:Spring是一个全面的Java企业级应用开发框架,提供DI(Dependency Injection)依赖注入、AOP...

    JAVA如何调用wsdl过程详解

    这种方式较为复杂,需要对wsdl有所了解,因此文档的重点在于第一种方式。 在使用cxf的wsdl2java工具生成本地类的过程中,文档详细说明了可能出现的问题及其解决方案: a. 首先,要注意JDK版本的选择。如果使用的是...

    第一个webservice例子源码

    【标题】"第一个Web服务(WebService)例子源码"提供了初学者深入了解和实践WebService开发的绝佳素材。在本文中,我们将深入探讨这个简单的"Hello World"程序如何工作,以及它如何帮助我们理解WebService的核心概念...

    cxf框架实例

    CXF允许开发者以编程或基于注解的方式创建Web服务,同时也支持WSDL(Web Service Description Language)第一类公民,可以从WSDL生成服务代码。 二、CXF与Webservice Web服务是一种通过网络进行通信的软件组件,它...

    xfire开发Web Service接口详解

    在"xfire开发Web Service接口第一个程序"这个文件中,你可能会找到一个简单的示例,展示如何创建一个计算两个数相加的Web Service。这个例子可能包括定义服务接口、实现接口、配置服务以及启动服务的步骤。通过分析...

    Axis2 例子

    1. **创建服务**:创建一个Java类,包含一个返回“Hello World”的方法。 2. **生成WSDL**:使用Apache CXF或其他工具生成对应的服务WSDL文件。 3. **部署服务**:将服务类和WSDL文件放在Axis2的services目录下。 4....

    xfire-webservice实例(下载即可运行)

    此外,Xfire还支持WSDL第一(WSDL-First)和Java第一(Java-First)两种开发模式,满足不同开发风格的需求。 总结来说,Xfire提供的这个实例是一个很好的学习和实践平台,它帮助我们理解Web服务的生命周期和工作...

    Java6开发WebService详细啊.docx

    在这个例子中,`doSomething`方法是Web Service的核心业务逻辑,它返回一个简单的字符串。`main`方法用于发布Web Service到指定的URL。`Endpoint.publish`方法接收两个参数:一个是服务的地址,另一个是实现Web ...

    Xfire webservice 例子

    【Xfire Webservice 例子详解】 Xfire 是一款曾经流行的游戏社交软件,它允许玩家在游戏中进行即时通讯、好友管理以及游戏统计等功能。然而,我们今天要讨论的是如何利用 Xfire 提供的 Web Service API 来开发自己...

Global site tag (gtag.js) - Google Analytics