webService修改样式

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   

相关文章
相关标签/搜索