JAX-WS(三)wsdl详解

http://localhost:8888/numberService?wsdl
<?xml version="1.0"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace="http://service.hqh.com/" name="MyServiceImplService">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://service.hqh.com/" schemaLocation="http://localhost:8888/numberService?xsd=1"/>
    </xsd:schema>
  </types>
  <message name="plus">
    <part name="parameters" element="tns:plus"/>
  </message>
  <message name="plusResponse">
    <part name="parameters" element="tns:plusResponse"/>
  </message>
  <message name="minus">
    <part name="parameters" element="tns:minus"/>
  </message>
  <message name="minusResponse">
    <part name="parameters" element="tns:minusResponse"/>
  </message>
  <portType name="IMyService">
    <operation name="plus">
      <input message="tns:plus"/>
      <output message="tns:plusResponse"/>
    </operation>
    <operation name="minus">
      <input message="tns:minus"/>
      <output message="tns:minusResponse"/>
    </operation>
  </portType>
  <binding name="MyServiceImplPortBinding" type="tns:IMyService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="plus">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <operation name="minus">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="MyServiceImplService">
    <port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
      <soap:address location="http://localhost:8888/numberService"/>
    </port>
  </service>
</definitions>


type节点分析
type用来定义访问的类型,即方法入参的个数,类型和返回值的类型
<definitions targetNamespace="http://service.hqh.com/" name="MyServiceImplService">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://service.hqh.com/" schemaLocation="http://localhost:8888/numberService?xsd=1"/>
    </xsd:schema>
  </types>
</definitions>


http://localhost:8888/numberService?xsd=1

<?xml version="1.0"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<xs:schema version="1.0" targetNamespace="http://service.hqh.com/">
  <!--方法A的参数-->
  <xs:element name="minus" type="tns:minus"/>
  <!--方法A的返回值-->
  <xs:element name="minusResponse" type="tns:minusResponse"/>
  <!--方法B的参数-->
  <xs:element name="plus" type="tns:plus"/>
  <!--方法B的返回值-->
  <xs:element name="plusResponse" type="tns:plusResponse"/>
  <!--方法A的参数信息-->
  <xs:complexType name="minus">
    <xs:sequence>
      <xs:element name="arg0" type="xs:int"/>
      <xs:element name="arg1" type="xs:int"/>
    </xs:sequence>
  <!--方法A的返回值信息-->
  </xs:complexType>
  <xs:complexType name="minusResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
  <!--方法B的参数信息-->
  <xs:complexType name="plus">
    <xs:sequence>
      <xs:element name="arg0" type="xs:int"/>
      <xs:element name="arg1" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
  <!--方法B的返回值信息-->
  <xs:complexType name="plusResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>


message节点分析
message节点用来传递消息---SOAP simple object access protocol
每个方法都有入参和返回值,这些数据就是通过message来进行传递的
客户端通过message节点传递参数到服务端,服务端执行方法,返回值又通过message节点来传递数据。一个方法至少对于2个message节点!
<?xml version="1.0"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace="http://service.hqh.com/" name="MyServiceImplService">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://service.hqh.com/" schemaLocation="http://localhost:8888/numberService?xsd=1"/>
    </xsd:schema>
  </types>
  <!--传递plus()的入参-->
  <message name="plus">
    <part name="parameters" element="tns:plus"/>
  </message>
  <!--传递plus()返回的消息-->
  <message name="plusResponse">
    <part name="parameters" element="tns:plusResponse"/>
  </message>
  <!--传递minus()的入参-->
  <message name="minus">
    <part name="parameters" element="tns:minus"/>
  </message>
  <!--传递minus()返回的消息-->
  <message name="minusResponse">
    <part name="parameters" element="tns:minusResponse"/>
  </message>
</definitions>


portType节点分析
portType指名对外暴露的接口名称
operation 指定被访问的方法
input 指定方法的入参(指定入参对应的message)
output 指定方法的返回值(指定返回值对应的message)
<?xml version="1.0"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace="http://service.hqh.com/" name="MyServiceImplService">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://service.hqh.com/" schemaLocation="http://localhost:8888/numberService?xsd=1"/>
    </xsd:schema>
  </types>
  <message name="plus">
    <part name="parameters" element="tns:plus"/>
  </message>
  <message name="plusResponse">
    <part name="parameters" element="tns:plusResponse"/>
  </message>
  <message name="minus">
    <part name="parameters" element="tns:minus"/>
  </message>
  <message name="minusResponse">
    <part name="parameters" element="tns:minusResponse"/>
  </message>
  <!-- 服务器端的接口 -->
  <portType name="IMyService">
    <!--接口中的方法-->
    <operation name="plus">
      <!--方法的入参通过name="tns:plus"的message传递-->
      <input message="tns:plus"/>
      <!--方法的返回值通过name="tns:plusResponse"的message传递-->
      <output message="tns:plusResponse"/>
    </operation>
    <operation name="minus">
      <input message="tns:minus"/>
      <output message="tns:minusResponse"/>
    </operation>
  </portType>
</definitions>



binding节点分析
bingding节点主要用于指定SOAP消息以何种方式/样式进行传递
<?xml version="1.0"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace="http://service.hqh.com/" name="MyServiceImplService">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://service.hqh.com/" schemaLocation="http://localhost:8888/numberService?xsd=1"/>
    </xsd:schema>
  </types>
  <message name="plus">
    <part name="parameters" element="tns:plus"/>
  </message>
  <message name="plusResponse">
    <part name="parameters" element="tns:plusResponse"/>
  </message>
  <message name="minus">
    <part name="parameters" element="tns:minus"/>
  </message>
  <message name="minusResponse">
    <part name="parameters" element="tns:minusResponse"/>
  </message>
  <portType name="IMyService">
    <operation name="plus">
      <input message="tns:plus"/>
      <output message="tns:plusResponse"/>
    </operation>
    <operation name="minus">
      <input message="tns:minus"/>
      <output message="tns:minusResponse"/>
    </operation>
  </portType>
  <!-- 指定SOAP消息传递所使用的格式 -->
  <binding name="MyServiceImplPortBinding" type="tns:IMyService">
    <!-- SOAP使用document的风格进行传递(原来使用RBC的风格) -->
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <!-- plus()的消息传递 -->
    <operation name="plus">
      <soap:operation soapAction=""/>
      <!--输入消息通过literal方式进行传递(jdk1.6之前使用SOAPMessage encode方式传递)-->
      <input>
        <soap:body use="literal"/>
      </input>
      <!--输出消息通过literal-->
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <!-- minus()的消息传递 -->
    <operation name="minus">
      <soap:operation soapAction=""/>
      <!--输入消息通过literal方式进行传递 -->
      <input>
        <soap:body use="literal"/>
      </input>
      <!--输出消息通过literal-->
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
</definitions>



service节点分析
结合前面的SOAP消息传输、接口和方法(参数和返回值)的说明、SOAP消息(message)来提供一个服务
通过该服务即可调用webservice对外提供的方法
<?xml version="1.0"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace="http://service.hqh.com/" name="MyServiceImplService">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://service.hqh.com/" schemaLocation="http://localhost:8888/numberService?xsd=1"/>
    </xsd:schema>
  </types>
  <message name="plus">
    <part name="parameters" element="tns:plus"/>
  </message>
  <message name="plusResponse">
    <part name="parameters" element="tns:plusResponse"/>
  </message>
  <message name="minus">
    <part name="parameters" element="tns:minus"/>
  </message>
  <message name="minusResponse">
    <part name="parameters" element="tns:minusResponse"/>
  </message>
  <portType name="IMyService">
    <operation name="plus">
      <input message="tns:plus"/>
      <output message="tns:plusResponse"/>
    </operation>
    <operation name="minus">
      <input message="tns:minus"/>
      <output message="tns:minusResponse"/>
    </operation>
  </portType>
  <binding name="MyServiceImplPortBinding" type="tns:IMyService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="plus">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <operation name="minus">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <!--指定服务的名称(同definitions的name属性)-->
  <service name="MyServiceImplService">
    <!--prot的name属性指定的端口(一个方法)将返回服务接口的一个实现,该实现基于name="MyServiceImplPortBinding"的SOAP消息定义进行创建-->
    <port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
      <!--指定服务对外发布的地址-->
      <soap:address location="http://localhost:8888/numberService"/>
    </port>
  </service>
</definitions>


SOAP
SOAP通过 "信封"传递消息
信封:携带方法的入参
<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
<!--对外发布的服务所在的包(逆序)-->
xmlns:q0="http://service.hqh.com/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <q0:plus>
      <arg0>1</arg0>
      <arg1>2</arg1>
    </q0:plus>
  </soapenv:Body>
</soapenv:Envelope>


信封:携带方法的返回值
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <ns2:plusResponse xmlns:ns2="http://service.hqh.com/">
      <return>3</return>
    </ns2:plusResponse>
  </S:Body>
</S:Envelope>


定制SOAP中的参数名
通过注解详细说明wsdl中的参数名称,否则默认使用args0,args1...来表示形参
服务接口中使用注解定义如下:
@WebService
public interface IMyService {
	@WebResult(name="plusResult")
	public int plus(@WebParam(name="a")int a,@WebParam(name="b")int b);
	
	@WebResult(name="minusResult")
	public int minus(@WebParam(name="a")int a,@WebParam(name="b")int b);
}



通过注解标注了参数后,SOAP中的参数名就是指定的名称了

<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:q0="http://service.hqh.com/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <q0:plus>
      <a>5</a> <!--信封中参数名称为注解所标注的名称-->
      <b>7</b> <!--信封中参数名称为注解所标注的名称-->
    </q0:plus>
  </soapenv:Body>
</soapenv:Envelope>

你可能感兴趣的:(jax-ws)