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

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值