webService修改樣式使用@SOAPBindingjava
@SOAPBinding(style = Style.RPC,parameterStyle = ParameterStyle.BARE,use = Use.ENCODED)web
該註解中有三個屬性值:設計模式
style:中有兩個靜態值 Style.DOCUMENT 和 Style.RPC。緩存
parameterStyle :中有兩個靜態值ParameterStyle.BARE和ParameterStyle.WRAPPEDapp
use :中有兩個靜態值 Use.ENCODED和Use.LITERALthis
webService默認使用:spa
@SOAPBinding(style = Style.DOCUMENT ,parameterStyle = ParameterStyle.WRAPPED,use = Use.LITERAL).net
參考:設計
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)的後續版本。code
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屬性值必須進行包裝,相關代碼
// 添加用戶
<span style="font-size:18px;">@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);</span>
<span style="font-size:18px;">其中,相應包裝類爲className = "cn.edu.ujn.dto.User",其具體內容以下:</span>
<span style="font-size:18px;">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;
}
}</span>
<span style="font-size:18px;">在進行參數名的替換時,會將localName = "userInfo"在className = "cn.edu.ujn.dto.User"中匹配,若匹配成功,則進行替換操做,替換後的效果能夠在wsdl文件中查看,以下圖1-1所示,不然編譯器會報如圖1-2所示的錯誤:</span>
圖1-1 wsdl文檔
圖1-2 錯誤提示
出現此錯誤的緣由正是由於所定義的變量userInfo1未存在於包裝類user中。
此種綁定形式的缺點是在進行參數初始化時需進行兩次new操做(分別爲in和out階段),浪費內存,固然能夠考慮使用工廠設計模式。
注:在進行重複部署服務的時候,應當將Tomcat容器中的web項目刪除,不然會由於緩存的緣由而看不到新部署的服務效果。
2)DocumentBare:
其指定形式以下:
<span style="font-size:18px;">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);
}</span>
<span style="font-size:18px;">其中,最重要的參數設置是紅色部分的內容。此種方式對於數據類型簡單如int、String、array類型的數據使用,對於list、map等集合複雜類型的數據不適用。此種形式的優勢是節省內存。</span>
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);
}
至此,示例代碼演示到此。但願朋友們能夠有所受益! --------------------- 做者:No Silver Bullet 來源:CSDN 原文:https://blog.csdn.net/sunhuaqiang1/article/details/44947269