Google Earth Engine ——非洲土壤表面的實際蒸發量數據集
922
2025-03-31
Web Service進階(五)SOAPBinding方式講解
Java API for XML Web Services (JAX-WS) 2.0 (JSR 224) Standard Implementation (SI)
JAX-WS2.0是JAX-RPC 1.1 (JSR 101)的后續版本。
1. JAX-WS 仍然支持 SOAP 1.1 over HTTP 1.1,因此互操作性將不會受到影響,仍然可以在網上傳遞相同的消息。
2. JAX-WS 仍然支持 WSDL 1.1,因此您所學到的有關該規范的知識仍然有用。WSDL 2.0 規范已經接近完成,但在 JAX-WS 2.0 相關工作結束時其工作仍在進行中。
3. JAX-RPC 和 JAX-WS 都支持 SOAP 1.1。JAX-WS 還支持 SOAP 1.2。
4. WSDL 1.1 規范在 HTTP 綁定中定義,這意味著利用此規范可以在不使用 SOAP 的情況下通過 HTTP 發送 XML 消息。
5. JAX-RPC 忽略了 HTTP 綁定。而 JAX-WS 添加了對其的支持。
6. JAX-RPC 支持 WS-I Basic Profile (BP) V1.0。JAX-WS 支持 BP 1.1。(WS-I 即 Web 服務互操作性組織。)
在JAX-WS時代,wscompile已經被wsimport與wsgen代替。wsimport用于導入wsdl并生成可移植性組件(artifact)wsgen生成編譯后的SEI并生成可移植性組件(artifact). 當前wsgen并不產生wsdl.WSDL在部署的時候產生。但通過配置項可讓wsgen產生wsdl。
wscompile主于用于早期的RPC,使用wscompile需要編寫一個config.xml文件,作為wscompile的輸入。
昨天在GF3上部署WebService,在webserivce上添加了SOAPBinding(style=Style.RPC),[這個annotation最好寫在類層次上,寫在方面層次上容易與出現與類出現沖突],結果部署失敗。后來發現寫成SOAPBinding(style=Style.RPC,use=literal)才可以。從Google上找到一點證據:RPC/encoded is not a supported style/use mode with JAX-WS 2.0. JAX-WS2.0 is fully compliant with the WS-I Basic Profile 1.1 which mandates literal mode. The supported style/use modes are: rpc/literal and document/literal.
JAX-WS 中的SoapBinding目前支持3種方式:
1)Document Wrapped(默認使用方式,由下面的錯誤可見):
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,use=SOAPBinding.Use.LITERAL,parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
2)Document Bare:
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,use=SOAPBinding.Use.LITERAL,parameterStyle=SOAPBinding.ParameterStyle.BARE)
3)RPC:
@SOAPBinding(style=SOAPBinding.Style.RPC,use=SOAPBinding.Use.LITERAL,parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
另外 The?java.util.Collection?classes cannot be used with rpc/literal or document/literal BARE style due to a limitation in JAXB. However, they do work in the default document/literal WRAPPED style
本文上半部分出自 “天下無賊” 博客,請務必保留此出處http://guojuanjun.blog.51cto.com/277646/1196736
1)Document Wrapped:
//它是一個注解,用在類上指定將此類發布成一個ws.
//修改目標空間,修改服務名,端口名.在wsdl那里的xml文件顯示對應的修改信息
@WebService(targetNamespace = "http://ujn.cn/",serviceName = "UserService", portName = "UserPort")
public interface UserService {
// 添加用戶
@WebMethod
@ResponseWrapper(localName = "add_Status", targetNamespace = "http://ujn.cn/", className = "cn.ujn.edu.dto.User")
@RequestWrapper(localName = "userInfo", targetNamespace = "http://ujn.cn/", className = "cn.ujn.edu.dto.User")
@WebResult(name="add_Status")
public int add(String userStr);
// 查找用戶
@WebMethod
@WebResult(name="login_Status")
public int login(String userStr);
}
剛開始寫注解的時候,感覺應該會很簡單,其實不然。對于這三種綁定方式,我們首先應該考慮應用場景,針對不同的應用場景選擇不同的綁定形式。后面會具體分析綁定方式的選擇問題。
在Document wrapped方式中,我們設置的@WebResult(name="add_Status")和@WebParam(name="userInfo")其中的name屬性值必須進行包裝,相關代碼
// 添加用戶
@WebMethod
@ResponseWrapper(localName = "add_Status", targetNamespace = "http://ujn.cn/", className = "cn.edu.ujn.dto.User")
@RequestWrapper(localName = "userInfo", targetNamespace = "http://ujn.cn/", className = "cn.edu.ujn.dto.User")
@WebResult(name="add_Status")
public int add(@WebParam(name="userInfo")String userStr);
其中,相應包裝類為className = "cn.edu.ujn.dto.User",其具體內容如下:
public class User {
private String userInfo;
private String login_Status;
public String getUserInfo() {
return userInfo;
}
public void setUserInfo(String userInfo) {
this.userInfo = userInfo;
}
public String getLogin_Status() {
return login_Status;
}
public void setLogin_Status(String login_Status) {
this.login_Status = login_Status;
}
}
在進行參數名的替換時,會將localName = "userInfo"在className = "cn.edu.ujn.dto.User"中匹配,若匹配成功,則進行替換操作,替換后的效果可以在wsdl文件中查看,如下圖1-1所示,否則編譯器會報如圖1-2所示的錯誤:
圖1-1 wsdl文檔
圖1-2 錯誤提示
出現此錯誤的原因正是因為所定義的變量userInfo1未存在于包裝類user中。
此種綁定形式的缺點是在進行參數初始化時需進行兩次new操作(分別為in和out階段),浪費內存,當然可以考慮使用工廠設計模式。
注:在進行重復部署服務的時候,應當將Tomcat容器中的web項目刪除,否則會因為緩存的原因而看不到新部署的服務效果。
2)DocumentBare:
其指定形式如下:
public interface UserService {
// 添加用戶
@WebMethod
@WebResult(name="add_Status")
public int add(@WebParam(name="userInfo")String userStr);
// 查找用戶
@WebMethod
@WebResult(name="login_Status")
public int login(@WebParam(name="userInfo")String userStr);
}
其中,最重要的參數設置是紅色部分的內容。此種方式對于數據類型簡單如int、String、array類型的數據使用,對于list、map等集合復雜類型的數據不適用。此種形式的優點是節省內存。
3)RPC:
其指定形式如下:
public?interface?UserService?{
//?添加用戶
@WebMethod
@WebResult(name="add_Status")
public?int?add(@WebParam(name="userInfo")String?userStr);
//?查找用戶
@WebMethod
@WebResult(name="login_Status")
public?int?login(@WebParam(name="userInfo")String?userStr);
}
至此,示例代碼演示到此。希望朋友們可以有所受益!
美文美圖
HTTP Spring web前端
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。