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

news/2024/7/10 16:23:47 标签: WebService, SOAP, Apache, Eclipse, Web

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


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


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


HelloWorld.java

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

HelloWorldImpl.java

package test;
import javax.jws.WebService>WebService;
@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>


http://www.niftyadmin.cn/n/1313316.html

相关文章

认识和使用OAuth2.0附OAuth实例

1.什么是OAuth2.0 OAuth是一个关于授权&#xff08;authorization&#xff09;的开放网络标准&#xff0c;在全世界得到广泛应用&#xff0c;目前的版本是2.0版。 OAuth(开放授权)是一个开放标准。允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息。而这…

PM/PD

PRD&#xff08;Product Requirements Document 产品需求文档&#xff09; PM、PD、UE与UI&#xff0c;分别是产品经理、产品设计师、用户体验师、视觉设计师四个角色。一般来说&#xff0c;这个顺序就是一个产品从规划到最终成型的任务流方向&#xff0c;是一个从抽象到具体、…

oracle(用户与表空间的管理)

数据洁癖 --1、创建临时表空间 create temporary tablespace oa_temp tempfile c:\data\oa_temp_001.dbf size 1024m extent management local; --2、创建用户数据表空间 create tablespace oa_data logging datafile c:\data\oa_data_001.dbf size 1024m reuse extent managem…

CXF的第一个例子 -- 深入剖析

Web Service 的 Client 和 Server 端 之间的通信 分为下面4步骤&#xff1a; 1、client 向 server 发送一个get请求&#xff0c;希望得到 web service 的 wsdl 文件。 2、server 传给 client wsdl 文件。 3、根据wsdl文件&#xff0c;client向server发送调用请求&#xff08…

SpringBoot 开启 Actuator

在生产环境中&#xff0c;需要实时或定期监控服务的可用性。spring-boot 的actuator&#xff08;监控&#xff09;功能提供了很多监控所需的接口。简单的配置和使用如下&#xff1a; 1、引入依赖&#xff1a; [html] view plain copy <dependency> <groupId>o…

如果关闭PHP的Notice/Warning这类提示

修改PHP.INI文件的配置即可。 下面是配置文件中的说明&#xff0c;按照对应的说明选择配置组合即可。 ------------修改ERROR报错的等级----------- ; Common Values: ; E_ALL (Show all errors, warnings and notices including coding standards.) ; E_ALL & ~E_NO…

heartbeat单独提供高可用服务

heartbeat单独提供高可用服务 本文目录&#xff1a;1.简介2.安装heartbeat 2.1 编译安装Heartbeat3.heartbeat相关配置文件 3.1 配置文件ha.cf 3.2 配置文件authkeys 3.3 配置文件haresources4.示例&#xff1a;heartbeat为httpd提供高可用服务 1.简介 heartbeat是人所众知…

CXF -- JAX-WS

JAX-WS Java API for XML Web Service 含有两个包&#xff1a; javax.jws javax.jws.soap 在第一个例子中&#xff0c;接口的定义为 WebService public interface HelloWorld { public WebResult(name"sayHelloResult")String sayHello(WebParam(name&q…