webservice系统学习笔记9-使用契约优先的方式的一个服务端demo(隐式传Header信息)...
生活随笔
收集整理的這篇文章主要介紹了
webservice系统学习笔记9-使用契约优先的方式的一个服务端demo(隐式传Header信息)...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
服務器端:
1、編寫wsdl文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/mywsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MyServiceImplService" targetNamespace="http://www.example.org/mywsdl/"><!-- 定義類型 --><wsdl:types><xsd:schema targetNamespace="http://www.example.org/mywsdl/"><xsd:element name="add" type="tns:add"/><xsd:element name="addResponse" type="tns:addResponse"/><xsd:element name="divide" type="tns:divide"/><xsd:element name="divideResponse" type="tns:divideResponse"/><xsd:element name="licenseInfo" type="xsd:string"/><xsd:complexType name="add"><xsd:sequence><xsd:element name="a" type="xsd:int"/><xsd:element name="b" type="xsd:int"/></xsd:sequence></xsd:complexType><xsd:complexType name="addResponse"><xsd:sequence><xsd:element name="addResult" type="xsd:int"/></xsd:sequence></xsd:complexType><xsd:complexType name="divide"><xsd:sequence><xsd:element name="num1" type="xsd:int"/><xsd:element name="num2" type="xsd:int"/></xsd:sequence></xsd:complexType><xsd:complexType name="divideResponse"><xsd:sequence><xsd:element name="divideResult" type="xsd:int"/></xsd:sequence></xsd:complexType></xsd:schema></wsdl:types><!-- 定義消息 --><wsdl:message name="add"><wsdl:part name="add" element="tns:add"/></wsdl:message><wsdl:message name="addResponse"><wsdl:part name="addResponse" element="tns:addResponse"></wsdl:part></wsdl:message><wsdl:message name="divide"><wsdl:part name="divide" element="tns:divide"/></wsdl:message><wsdl:message name="divideResponse"><wsdl:part name="divideResponse" element="tns:divideResponse"/></wsdl:message><!-- 頭消息 --><wsdl:message name="licenseInfo"><wsdl:part name="licenseInfo" element="tns:licenseInfo"></wsdl:part></wsdl:message><!-- 定義port --><wsdl:portType name="IMyService"><wsdl:operation name="add"><wsdl:input message="tns:add"/><wsdl:output message="tns:addResponse"/></wsdl:operation><wsdl:operation name="divide"><wsdl:input message="tns:divide"/><wsdl:output message="tns:divideResponse"/></wsdl:operation></wsdl:portType><!-- 綁定服務 --><wsdl:binding name="myServiceSOAP" type="tns:IMyService"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="add"><wsdl:input><soap:body use="literal"/><!-- 為add添加隱式的頭信息 --><soap:header use="literal" part="licenseInfo" message="tns:licenseInfo"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="divide"><wsdl:input><soap:body use="literal"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><!-- 發布服務 --><wsdl:service name="MyServiceImplService"><!-- 這里的名字必須和<wsdl:definitions里的name一致 --><wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort"><soap:address location="http://localhost:8989/ms"/></wsdl:port></wsdl:service> </wsdl:definitions>?
2、使用wsimport命令生成服務端接口類(IMyService.java)
備注:生成的其他文件都可以刪除
在add方法的參數定義上加上
@WebParam(name="licenseInfo")String licenseInfo?
3、編寫服務接口的實現類MyServiceImpl.java
package org.example.mywsdl;import javax.jws.WebService;//這里必須制定wsdlLocation來約定參數等名稱等 //endpointInterface可寫也可不寫,寫它主要是用到自動生成的一些注解信息 @WebService(endpointInterface="org.example.mywsdl.IMyService",targetNamespace="http://www.example.org/mywsdl/",wsdlLocation="META-INF/wsdl/mywsdl.wsdl",name="MyServiceImpl") public class MyServiceImpl implements IMyService {@Overridepublic int add(int a, int b, String licenseInfo) {return a+b;}@Overridepublic int divide(int num1, int num2) {return num1/num2;}}?
4、服務啟動類
package org.example.mywsdl;import javax.xml.ws.Endpoint;public class RunService {public static void main(String[] args) {//這里的發布地址必須和wsdl中定義的一樣Endpoint.publish("http://localhost:8989/ms", new MyServiceImpl());} }?
訪問服務:http://localhost:8989/ms?wsdl
?
客戶端直接用上面的地址生成即可,生成的wsdl文件即是我們編寫的那個wsdl文件
<?xml version="1.0" encoding="UTF-8"?><!-- 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. --><wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/mywsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MyServiceImplService" targetNamespace="http://www.example.org/mywsdl/"><!-- 定義類型 --><wsdl:types><xsd:schema targetNamespace="http://www.example.org/mywsdl/"><xsd:element name="add" type="tns:add"></xsd:element><xsd:element name="addResponse" type="tns:addResponse"></xsd:element><xsd:element name="divide" type="tns:divide"></xsd:element><xsd:element name="divideResponse" type="tns:divideResponse"></xsd:element><xsd:element name="licenseInfo" type="xsd:string"></xsd:element><xsd:complexType name="add"><xsd:sequence><xsd:element name="a" type="xsd:int"></xsd:element><xsd:element name="b" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType><xsd:complexType name="addResponse"><xsd:sequence><xsd:element name="addResult" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType><xsd:complexType name="divide"><xsd:sequence><xsd:element name="num1" type="xsd:int"></xsd:element><xsd:element name="num2" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType><xsd:complexType name="divideResponse"><xsd:sequence><xsd:element name="divideResult" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType></xsd:schema></wsdl:types><!-- 定義消息 --><wsdl:message name="add"><wsdl:part name="add" element="tns:add"></wsdl:part></wsdl:message><wsdl:message name="addResponse"><wsdl:part name="addResponse" element="tns:addResponse"></wsdl:part></wsdl:message><wsdl:message name="divide"><wsdl:part name="divide" element="tns:divide"></wsdl:part></wsdl:message><wsdl:message name="divideResponse"><wsdl:part name="divideResponse" element="tns:divideResponse"></wsdl:part></wsdl:message><!-- 頭消息 --><wsdl:message name="licenseInfo"><wsdl:part name="licenseInfo" element="tns:licenseInfo"></wsdl:part></wsdl:message><!-- 定義port --><wsdl:portType name="IMyService"><wsdl:operation name="add"><wsdl:input message="tns:add"></wsdl:input><wsdl:output message="tns:addResponse"></wsdl:output></wsdl:operation><wsdl:operation name="divide"><wsdl:input message="tns:divide"></wsdl:input><wsdl:output message="tns:divideResponse"></wsdl:output></wsdl:operation></wsdl:portType><!-- 綁定服務 --><wsdl:binding name="myServiceSOAP" type="tns:IMyService"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding><wsdl:operation name="add"><wsdl:input><soap:body use="literal"></soap:body><!-- 為add添加隱式的頭信息 --><soap:header use="literal" part="licenseInfo" message="tns:licenseInfo"></soap:header></wsdl:input><wsdl:output><soap:body use="literal"></soap:body></wsdl:output></wsdl:operation><wsdl:operation name="divide"><wsdl:input><soap:body use="literal"></soap:body></wsdl:input><wsdl:output><soap:body use="literal"></soap:body></wsdl:output></wsdl:operation></wsdl:binding><!-- 發布服務 --><wsdl:service name="MyServiceImplService"><!-- 這里的名字必須和<wsdl:definitions里的name一致 --><wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort"><soap:address location="http://localhost:8989/ms"></soap:address></wsdl:port></wsdl:service> </wsdl:definitions>?
使用該wsdl生成客戶端java代碼后的add方法如下:
@WebMethod@WebResult(name = "addResult", targetNamespace = "")@RequestWrapper(localName = "add", targetNamespace = "http://www.example.org/mywsdl/", className = "com.ws04.Add")@ResponseWrapper(localName = "addResponse", targetNamespace = "http://www.example.org/mywsdl/", className = "com.ws04.AddResponse")public int add(@WebParam(name = "a", targetNamespace = "") int a,@WebParam(name = "b", targetNamespace = "") int b);此時方法的參數里沒有看到有頭信息參數的定義,調用該方法可以使用handler或者soap來編碼傳遞頭信息
總結
以上是生活随笔為你收集整理的webservice系统学习笔记9-使用契约优先的方式的一个服务端demo(隐式传Header信息)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机内置天线
- 下一篇: C51存储器类型与51单片机的物理区域