WebService--了解wsdl文件

schema约束文件

作用:约束xml的文件格式和可以包含哪些节点
说明:schema文件中的节点名称和结构是由w3c组织定义

规范:

  1. 所有标签和属性都需要有schema文件定义
  2. 所有的schema文件都需要有一个id,但在这里它叫namespace
  3. namespace由targetNamespace属性来定义,targetNamespace的值是一个url(此属性很大可能不存在)
  4. 如何引入一个schema文件:通过xml的属性xmlns,属性值为schema文件的id,即namespace值,即argetNamespace属性的值,
  5. 如果引入的schema文件不会w3c组织定义,则必须指定schema文件的位置
  6. schema文件的位置由schemaLocation属性定义,值为schema文件的namespace的值
  7. 如果引入n个约束,需要给约束取n-1个别名,名别是通过xmlns加上冒号,即xmlns:起的别名=“约束文件的地址”

获取到的wsdl文件(以CXF发布的服务为例)

以下说的节点标签都是去掉wsdl的

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.webservice.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://webservice.com/" name="UserServiceImplService" targetNamespace="http://impl.webservice.com/">
    <wsdl:import location="http://localhost:8000/user?wsdl=IUserService.wsdl" namespace="http://webservice.com/"> </wsdl:import>
    <wsdl:binding name="UserServiceImplServiceSoapBinding" type="ns1:IUserService">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="sayHi">
            <soap:operation soapAction="" style="document"/>
            <wsdl:input name="sayHi">
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="sayHiResponse">
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="UserServiceImplService">
        <wsdl:port binding="tns:UserServiceImplServiceSoapBinding" name="UserServiceImplPort">
            <soap:address location="http://localhost:8000/user"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

复制import节点的location的值在浏览器中打开进一步获取详细的wsdl文件

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://webservice.com/" name="IUserService" targetNamespace="http://webservice.com/">
    <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.com/" elementFormDefault="unqualified" targetNamespace="http://webservice.com/" version="1.0">
            <xs:element name="sayHi" type="tns:sayHi"/>
            <xs:element name="sayHiResponse" type="tns:sayHiResponse"/>
            <xs:complexType name="sayHi">
                <xs:sequence>
                    <xs:element minOccurs="0" name="arg0" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="sayHiResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="sayHiResponse">
        <wsdl:part element="ns1:sayHiResponse" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:message name="sayHi">
        <wsdl:part element="ns1:sayHi" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="IUserService">
        <wsdl:operation name="sayHi">
            <wsdl:input message="ns1:sayHi" name="sayHi"> </wsdl:input>
            <wsdl:output message="ns1:sayHiResponse" name="sayHiResponse"> </wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
</wsdl:definitions>

一般这两个wsdl文件都是访问一次性是一个整体出来的,在这以两个文件出来经看其他博客是因为第一个wsdl文件的wsdl:definitions标签内的targetNamespace属性和wsdl:import中namespace属性的值不同。
解决方案:
1,将接口类和实现类放在同一个包下,问题即可解决
2,将接口类和实现类中的注解中加入命名空间属性配置,@WebService(targetNamespace=“XXXXX”),两个配置成一样即可,或者接口的配置成实现类的默认值、实现类的配置成接口的默认值也可解决问题。

文档节点

  1. definitions

1. definitions

所有WSDL文档的根节点都是definition节点
(1) definition元素中一般包括若干个XML命名空间;
http://schemas.xmlsoap.org/wsdl/是默认的命名空间,这样就可以不用显式地定义每一个WSDL元素的命名空间了,例如:<types> <messages> <portType>…;文档中所有的元素缺省应该属于这个命名空间。
(2)definition元素的的一个属性是name,此属性为访问接口实现类的类名加上了Service;
(3)targetNamespace属性是访问接口实现类的包名。

2. import

Import元素可以让当前的文档使用其他WSDL文档中指定命名空间中的定义。 必须声明两个属性,即namespace属性和location属性。namespace属性必须和正导入的WSDL文档中声明的targetNamespace相匹配。一般为域名,即包名的倒置。location属性必须指向一个实际的WSDL文档。可以通过此节点的location属性的值再次发送请求获取wsdl文件

3. binding

Binding元素将一个抽象的portType映射到一组具体的协议(SOAP或者HTTP)、消息传递样式(RPC或者document)以及编码样式(literal或者SOAP encoding)。binding 元素有两个属性 - name 属性和 type 属性。name 属性定义 binding 的名称,而 type 属性指向用于 binding 的端口(值为服务接口类名)

<wsdl:binding name="UserServiceImplServiceSoapBinding" type="ns1:IUserService">
soap:binding元素

soap:binding元素指定了用于传输SOAP消息的Internet协议以及operation缺省的消息类型(RPC还是document)

<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
operation(name属性值是请求方法名)

operation元素指定了消息传递样式(RPC或者document),并且指定了SOAPAction字段的值(访问接口的方法名)。

<wsdl:operation name="sayHi">
            <soap:operation soapAction="" style="document"/>

4. types(同样包含请求参数和响应参数)

此内容包含了定义处理请求的接口中的函数名,参数类名和返回值类型其中sayHiResponse是函数名自动加上Response生成的,在实际中并没有。此文档中targetNamespace必须是一个有效的非空值,而且必须属于由WSDL文档。

<wsdl:types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.com/" elementFormDefault="unqualified" targetNamespace="http://webservice.com/" version="1.0">
        <xs:element name="sayHi" type="tns:sayHi"/>
        <xs:element name="sayHiResponse" type="tns:sayHiResponse"/>
        <xs:complexType name="sayHi">
            <xs:sequence>
                <xs:element minOccurs="0" name="arg0" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="sayHiResponse">
            <xs:sequence>
                <xs:element minOccurs="0" name="return" type="xs:int"/>
            </xs:sequence>
        </xs:complexType>
    </xs:schema>
</wsdl:types>

5. message(请求和响应参数)

通信消息的数据结构的抽象类型化定义。引用types中定义的标签,包含请求和响应的参数名称和参数类型,此节点的name属性是请求方法加上类型,如请求是请求方法加上Request,此方法的响应是方法名加上Response

6. portType

对于某个访问入口点类型所支持的操作的抽象集合,这些操作可以由一个或多个服务访问点来支持。

友情提醒

如果解析soup协议返回的报文或者解析wsdl文件,我们会看到xml文件的节点会有soup开头的和wsdl开头的,但是解析的时候获取节点不用加上那个,只要后面的名称,如wsdl:import节点,解析是用import获取才能获取到

请求需要的信息

  1. 请求地址带?wsdl的
  2. 命名空间:属性targetNamespace的值
  3. 请求方法:上面operation节点的name属性值
  4. 请求参数:上面types节点或者message节点的信息

参考博客:
https://blog.csdn.net/fengspg/article/details/7404383

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
生成 WSDL 文件是使用 JAX-WS API 的一个常见任务。以下是使用 JAX-WS API 生成 WSDL 文件的步骤: 1. 创建一个 Java 类,该类将充当 Web 服务的实现。这个实现类应该有一个无参构造函数,并且应该被注释为 @WebService。 2. 使用 JAX-WS API 的 Endpoint 类来发布 Web 服务。在发布 Web 服务时,您需要指定 Web 服务的地址和实现类。例如,以下代码发布了 Web 服务,该服务使用 MyServiceImpl 类作为其实现,并在 localhost 的端口 8080 上运行: ```java MyServiceImpl myService = new MyServiceImpl(); String address = "http://localhost:8080/myservice"; Endpoint.publish(address, myService); ``` 3. 在 Web 服务发布后,您可以使用 JAX-WS API 的 WSDLGenerator 类生成 WSDL 文件。以下是使用 WSDLGenerator 生成 WSDL 文件的代码: ```java WSDLFactory factory = WSDLFactory.newInstance(); WSDLGenerator wsdlGenerator = factory.newWSDLGenerator(); StringWriter writer = new StringWriter(); wsdlGenerator.generateWSDL(MyServiceImpl.class, new StreamResult(writer)); String wsdl = writer.toString(); System.out.println(wsdl); ``` 在这段代码中,我们首先创建了一个 WSDLFactory 实例,然后使用该工厂创建一个 WSDLGenerator 实例。接下来,我们创建一个 StringWriter 对象,以便将生成的 WSDL 文件写入字符串。最后,我们调用 WSDLGenerator 的 generateWSDL() 方法来生成 WSDL 文件,并将其写入 StringWriter 中。 以上就是使用 JAX-WS API 生成 WSDL 文件的详细步骤和代码。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值