当前位置 : 主页 > 网页制作 > Nodejs >

CXF创建WebService的一些注解

来源:互联网 收集:自由互联 发布时间:2021-06-24
Defining the Binding Properties with Annotations(定义binding的一些属性) If you are using a SOAP binding for your service, you can use JAX-WS annotations to specify a number of the bindings properties. These properties correspond dire
  1. Defining the Binding Properties with Annotations(定义binding的一些属性)
    If you are using a SOAP binding for your service, you can use JAX-WS annotations to specify a number of the bindings properties. These properties correspond directly to the properties you can specify in a service’s WSDL contract.
    可以给binding一些特殊的属性,这个在昨天的生成的wsdl文件中好像是没有进行介绍,今天就继续的聊聊这个到底是怎么玩的
    具体我也不是很清楚,慢慢的跟着文档学习,其实最好的实践就是看文档,每个人写的博客都不一样,毕竟在不同的一个层次,看重的关键的点子不是一样的。
<wsdl:binding name="HelloWSServiceSoapBinding" type="tns:HelloWS ">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="welcome">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="welcome">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="welcomeResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWSService">
<wsdl:port binding="tns:HelloWSServiceSoapBinding" name="HelloWS Port">
<soap:address location="http://localhost:8000/services/hello"/>
</wsdl:port>
</wsdl:service>
  1. The @SOAPBinding annotation is defined by the javax.jws.soap.SOAPBinding interface. It provides details about the SOAP binding used by the service when it is deployed. If the @SOAPBinding annotation is not specified, a service is published using a wrapped doc/literal SOAP binding.
    (这个注解被定义在这个接口里面的,这个注解详细的告知发布的WebService 服务的SOAP binding的信息字段,如果没有定义就使用默认的doc/literalSOAP binding信息字段 )

  2. You can put the @SOAPBinding annotation on the SEI and any of the SEI’s methods. When it is used on a method, setting of the method’s @SOAPBinding annotation take precedent.(可以绑定在接口上,也可以绑定在具体的方法上)

  3. 我们来看看怎么定义这个注解的,不会看注解的可以去学习一下,虽然不用自己写,还是多少懂一点比较好
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface SOAPBinding {
    SOAPBinding.Style style() default SOAPBinding.Style.DOCUMENT;
/** Specifies the style of the SOAP message. If RPC style is specified, each message part within the SOAP body is a parameter or return value and will appear inside a wrapper element within the soap:body element. The message parts within the wrapper element correspond to operation parameters and must appear in the same order as the parameters in the operation. If DOCUMENT style is specified, the contents of the SOAP body must be a valid XML document, but its form is not as tightly constrained. (DOCUMENT 形式不是很严格) **/
    SOAPBinding.Use use() default SOAPBinding.Use.LITERAL;
/** Specifies how the data of the SOAP message is streamed.定义数据流的形式 ENCODED:译成密码 LITERAL:照字面的;原义的,不进行加密处理 **/
    SOAPBinding.ParameterStyle parameterStyle() default SOAPBinding.ParameterStyle.WRAPPED;
/** Specifies how the method parameters, (方法的参数) which correspond to message parts in a WSDL contract, are placed into the SOAP message body. A parameter style of BARE(光秃秃的) means that each parameter the message root. A parameter style of WRAPPED means that all of the input parameters are wrapped into a single element on a into a single element in the response message. If you set the style to RPC you must use the WRAPPED parameter style. (感觉差距不大,自己想玩自己编程试试就知道这个到底是什么意思了) **/
    public static enum ParameterStyle {
        BARE,
        WRAPPED;

        private ParameterStyle() {
        }
    }

    public static enum Style {
        DOCUMENT,
        RPC;

        private Style() {
        }
    }

    public static enum Use {
        LITERAL,
        ENCODED;

        private Use() {
        }
    }
}
  1. Defining Operation Properties with Annotations(操作–>method)
    The @WebMethod annotation is defined by the javax.jws.WebMethod interface.
    It is placed on the methods in the SEI.(在接口中定义的)
    represented in the wsdl:operation (wsdl中呈现的位置就是这里的)element describing the operation to which the method is associated.
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface WebMethod {
    String operationName() default "";
  //方法的名称,默认为当前方法的名称
    String action() default "";
/** Specifies the value of the soapAction attribute of the soap:operation element generated for the method. The default value is an empty string. (就是在操作方法下面生成的一个子元素,默认为空) **/
    boolean exclude() default false;
   /** Specifies if the method should be excluded from the service interface. The default is false.(从接口方法中排除,默认为false,感觉没有啥子用处啊! ) **/
}
  1. The @RequestWrapper annotation
    The @RequestWrapper annotation is defined by the javax.xml.ws.RequestWrapper interface. It is placed on the methods in the SEI. (放置在接口的方法上的)As the name implies(暗示), @RequestWrapper specifies the Java class that implements the wrapper bean for the method parameters(对于请求的方法参数的包装) that are included in the request message sent in a remote invocation. It is also used to specify the element names, and namespaces, used by the runtime when marshalling and unmarshalling the request messages.(它还用于指定元素名称和命名空间,通过运行时的编组和解组请求消息中使用。)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestWrapper {
    /** * Element's local name. */
    public String localName() default "";
/** Specifies the local name of the wrapper element in the XML representation of the request message. The default value is the name of the method or the value of the @WebMethod annotation's operationName property.(默认为方法中的名称) **/
    /** Specifies the namespace under which the XML wrapper element is defined. The default value is the target namespace of the SEI. */
    public String targetNamespace() default "";

    /** * Specifies the full name of the Java class that implements the wrapper element. * 这个定义是必须的! */
    public String className() default "";

    /** * wsdl:part name for the wrapper part * * @since JAX-WS 2.2 */
    public String partName() default "";

}

7.The @ResponseWrapper annotation
The @ResponseWrapper annotation is defined by the javax.xml.ws.ResponseWrapper interface.
It is placed on the methods in the SEI.(定义在方法和上的) As the name implies,
@ResponseWrapper specifies the Java class that implements the wrapper bean for the method parameters that are included in the response message sent in a remote invocation(定义在方法的返回值上,在远程调用的时候作为返回值). It is also used to specify the element names, and namespaces, used by the runtime when marshalling and unmarshalling the response messages.(这个和上面的类似,主要用法)
Only the className property is required.

这个下面是apache 中的例子,我在使用的时候不用包装也是可以的。使用起来还是挺麻烦的。

package org.apache.cxf;

import javax.jws.*;
import javax.xml.ws.*;

@WebService(name="quoteReporter")
public interface QuoteReporter {
  @WebMethod(operationName="getStockQuote")
  @RequestWrapper(targetNamespace="http://demo.mycompany.com/types",
      className="java.lang.String")
  @ResponseWrapper(targetNamespace="http://demo.mycompany.com/types",
      className="org.eric.demo.Quote")
  public Quote getQuote(String ticker);
}

8.The @WebParam annotation
The @WebParam annotation is defined by the javax.jws.WebParam interface. It is placed on the parameters on the methods defined in the SEI.(放置在方法上的) The @WebParam annotation allows you to specify the direction of the parameter(特殊的定义参数), if the parameter will be placed in the SOAP header, and other properties of the generated wsdl:part.

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface WebParam {
    String name() default "";
/** Specifies the name of the parameter as it appears in the WSDL. For RPC bindings, this is name of the wsdl:part representing the parameter. For document bindings, this is the local name of the XML element representing the parameter. Per the JAX-WS specification, the default is argN, where N is replaced with the zero-based argument index (i.e., arg0, arg1, etc.) **/
    String partName() default "";
    /** Specifies the value of the name attribute of the wsdl:part element for the parameter when the binding is document. **/

    String targetNamespace() default "";

    WebParam.Mode mode() default WebParam.Mode.IN; 
    /** Specifies the direction of the parameter. 有点类似于引用传参数,这个哥哥尝试了一下,真的可以诶,INOUT 传入,传出参数。 **/
    boolean header() default false;
/** Specifies if the parameter is passed as part of the SOAP header. **/
    public static enum Mode {
        IN,
        OUT,
        INOUT;

        private Mode() {
        }
    }
}
网友评论