Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.Este contenido no está disponible en el idioma seleccionado.
23.2. Annotating the Code
- The target namespace for the service.
- The name of the class used to hold the request message
- The name of the class used to hold the response message
- If an operation is a one way operation
- The binding style the service uses
- The name of the class used for any custom exceptions
- The namespaces under which the types used by the service are defined
23.2.1. Required Annotations Copiar enlaceEnlace copiado en el portapapeles!
Overview Copiar enlaceEnlace copiado en el portapapeles!
@WebService annotation on both the SEI and the implementation class.
The @WebService annotation Copiar enlaceEnlace copiado en el portapapeles!
@WebService annotation is defined by the javax.jws.WebService interface and it is placed on an interface or a class that is intended to be used as a service. @WebService has the properties described in Table 23.1, “@WebService Properties”
| Property | Description |
|---|---|
| name | Specifies the name of the service interface. This property is mapped to the name attribute of the wsdl:portType element that defines the service's interface in a WSDL contract. The default is to append PortType to the name of the implementation class. [a] |
| targetNamespace | Specifies the target namespace where the service is defined. If this property is not specified, the target namespace is derived from the package name. |
| serviceName | Specifies the name of the published service. This property is mapped to the name attribute of the wsdl:service element that defines the published service. The default is to use the name of the service's implementation class. [a] |
| wsdlLocation | Specifies the URL where the service's WSDL contract is stored. This must be specified using a relative URL. The default is the URL where the service is deployed. |
| endpointInterface | Specifies the full name of the SEI that the implementation class implements. This property is only specified when the attribute is used on a service implementation class. |
| portName | Specifies the name of the endpoint at which the service is published. This property is mapped to the name attribute of the wsdl:port element that specifies the endpoint details for a published service. The default is the append Port to the name of the service's implementation class.[a] |
[a]
When you generate WSDL from an SEI the interface's name is used in place of the implementation class' name.
| |
@WebService annotation's properties. However, it is recommended that you provide as much information as you can.
Annotating the SEI Copiar enlaceEnlace copiado en el portapapeles!
@WebService annotation. Because the SEI is the contract that defines the service, you should specify as much detail as possible about the service in the @WebService annotation's properties.
@WebService Annotation” shows the interface defined in Example 23.1, “Simple SEI” with the @WebService annotation.
Example 23.3. Interface with the @WebService Annotation
@WebService annotation in Example 23.3, “Interface with the @WebService Annotation” does the following:
- 1
- Specifies that the value of the
nameattribute of thewsdl:portTypeelement defining the service interface isquoteUpdater. - 2
- Specifies that the target namespace of the service is http:\\demos.redhat.com.
- 3
- Specifies that the value of the
nameof thewsdl:serviceelement defining the published service isupdateQuoteService. - 4
- Specifies that the service will publish its WSDL contract at http:\\demos.redhat.com\quoteExampleService?wsdl.
- 5
- Specifies that the value of the
nameattribute of thewsdl:portelement defining the endpoint exposing the service isupdateQuotePort.
Annotating the service implementation Copiar enlaceEnlace copiado en el portapapeles!
@WebService annotation, you also must annotate the service implementation class with the @WebService annotation. When adding the annotation to the service implementation class you only need to specify the endpointInterface property. As shown in Example 23.4, “Annotated Service Implementation Class” the property must be set to the full name of the SEI.
Example 23.4. Annotated Service Implementation Class
23.2.2. Optional Annotations Copiar enlaceEnlace copiado en el portapapeles!
@WebService annotation is sufficient for service enabling a Java interface or a Java class, it does not fully describe how the service will be exposed as a service provider. The JAX-WS programming model uses a number of optional annotations for adding details about your service, such as the binding it uses, to the Java code. You add these annotations to the service's SEI.
23.2.2.1. Defining the Binding Properties with Annotations Copiar enlaceEnlace copiado en el portapapeles!
Overview Copiar enlaceEnlace copiado en el portapapeles!
The @SOAPBinding annotation Copiar enlaceEnlace copiado en el portapapeles!
@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.
@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 precedence.
@SOAPBinding Properties” shows the properties for the @SOAPBinding annotation.
| Property | Values | Description |
|---|---|---|
| style | Style.DOCUMENT (default)
Style.RPC
| 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 appears 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. |
| use | Use.LITERAL (default)
Use.ENCODED[a]
| Specifies how the data of the SOAP message is streamed. |
| parameterStyle [b] | ParameterStyle.BARE
ParameterStyle.WRAPPED (default)
| Specifies how the method parameters, which correspond to message parts in a WSDL contract, are placed into the SOAP message body. If BARE is specified, each parameter is placed into the message body as a child element of the message root. If WRAPPED is specified, all of the input parameters are wrapped into a single element on a request message and all of the output parameters are wrapped into a single element in the response message. |
[a]
Use.ENCODED is not currently supported.
| ||
Document bare style parameters Copiar enlaceEnlace copiado en el portapapeles!
@SOAPBinding annotation with its style property set to Style.DOCUMENT, and its parameterStyle property set to ParameterStyle.BARE.
- The operation must have no more than one input or input/output parameter.
- If the operation has a return type other than void, it must not have any output or input/output parameters.
- If the operation has a return type of void, it must have no more than one output or input/output parameter.
@WebParam annotation or the @WebResult annotation are not counted against the number of allowed parameters.
Document wrapped parameters Copiar enlaceEnlace copiado en el portapapeles!
@SOAPBinding annotation with its style property set to Style.DOCUMENT, and its parameterStyle property set to ParameterStyle.WRAPPED.
@RequestWrapper annotation” annotation and the the section called “The @ResponseWrapper annotation” annotation.
Example Copiar enlaceEnlace copiado en el portapapeles!
Example 23.5. Specifying a Document Bare SOAP Binding with the SOAP Binding Annotation
23.2.2.2. Defining Operation Properties with Annotations Copiar enlaceEnlace copiado en el portapapeles!
Overview Copiar enlaceEnlace copiado en el portapapeles!
- What the exchanged messages look like in XML
- If the message can be optimized as a one way message
- The namespaces where the messages are defined
The @WebMethod annotation Copiar enlaceEnlace copiado en el portapapeles!
@WebMethod annotation is defined by the javax.jws.WebMethod interface. It is placed on the methods in the SEI. The @WebMethod annotation provides the information that is normally represented in the wsdl:operation element describing the operation to which the method is associated.
@WebMethod Properties” describes the properties of the @WebMethod annotation.
The @RequestWrapper annotation Copiar enlaceEnlace copiado en el portapapeles!
@RequestWrapper annotation is defined by the javax.xml.ws.RequestWrapper interface. It is placed on the methods in the SEI. The @RequestWrapper annotation specifies the Java class implementing the wrapper bean for the method parameters of the request message starting a message exchange. It also specifies the element names, and namespaces, used by the runtime when marshalling and unmarshalling the request messages.
@RequestWrapper Properties” describes the properties of the @RequestWrapper annotation.
| Property | Description |
|---|---|
| localName | Specifies the local name of the wrapper element in the XML representation of the request message. The default value is either the name of the method, or the value of the the section called “The @WebMethod annotation” annotation's operationName property. |
| targetNamespace | Specifies the namespace under which the XML wrapper element is defined. The default value is the target namespace of the SEI. |
| className | Specifies the full name of the Java class that implements the wrapper element. |
@SOAPBinding annotation, and its parameterStyle property is set to ParameterStyle.BARE, this annotation is ignored.
The @ResponseWrapper annotation Copiar enlaceEnlace copiado en el portapapeles!
@ResponseWrapper annotation is defined by the javax.xml.ws.ResponseWrapper interface. It is placed on the methods in the SEI. The @ResponseWrapper specifies the Java class implementing the wrapper bean for the method parameters in the response message in the message exchange. It also specifies the element names, and namespaces, used by the runtime when marshaling and unmarshalling the response messages.
@ResponseWrapper Properties” describes the properties of the @ResponseWrapper annotation.
| Property | Description |
|---|---|
| localName | Specifies the local name of the wrapper element in the XML representation of the response message. The default value is either the name of the method with Response appended, or the value of the the section called “The @WebMethod annotation” annotation's operationName property with Response appended. |
| targetNamespace | Specifies the namespace where the XML wrapper element is defined. The default value is the target namespace of the SEI. |
| className | Specifies the full name of the Java class that implements the wrapper element. |
@SOAPBinding annotation and its parameterStyle property is set to ParameterStyle.BARE, this annotation is ignored.
The @WebFault annotation Copiar enlaceEnlace copiado en el portapapeles!
@WebFault annotation is defined by the javax.xml.ws.WebFault interface. It is placed on exceptions that are thrown by your SEI. The @WebFault annotation is used to map the Java exception to a wsdl:fault element. This information is used to marshall the exceptions into a representation that can be processed by both the service and its consumers.
@WebFault Properties” describes the properties of the @WebFault annotation.
The @Oneway annotation Copiar enlaceEnlace copiado en el portapapeles!
@Oneway annotation is defined by the javax.jws.Oneway interface. It is placed on the methods in the SEI that will not require a response from the service. The @Oneway annotation tells the run time that it can optimize the execution of the method by not waiting for a response and by not reserving any resources to process a response.
- They return void
- They have no parameters that implement the
Holderinterface - They do not throw any exceptions that can be passed back to a consumer
Example Copiar enlaceEnlace copiado en el portapapeles!
Example 23.6. SEI with Annotated Methods
23.2.2.3. Defining Parameter Properties with Annotations Copiar enlaceEnlace copiado en el portapapeles!
Overview Copiar enlaceEnlace copiado en el portapapeles!
wsdl:message elements and their wsdl:part elements. JAX-WS provides annotations that allow you to describe the wsdl:part elements that are generated for the method parameters.
The @WebParam annotation Copiar enlaceEnlace copiado en el portapapeles!
@WebParam annotation is defined by the javax.jws.WebParam interface. It is placed on the parameters of 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.
@WebParam Properties” describes the properties of the @WebParam annotation.
| Property | Values | Description |
|---|---|---|
| name | Specifies the name of the parameter as it appears in the generated WSDL document. For RPC bindings, this is the 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.). | |
| targetNamespace | Specifies the namespace for the parameter. It is only used with document bindings where the parameter maps to an XML element. The default is to use the service's namespace. | |
| mode | Mode.IN (default)[a]
Mode.OUT
Mode.INOUT
| Specifies the direction of the parameter. |
| header | false (default)
true
| Specifies if the parameter is passed as part of the SOAP header. |
| partName | Specifies the value of the name attribute of the wsdl:part element for the parameter. This property is used for document style SOAP bindings. | |
The @WebResult annotation Copiar enlaceEnlace copiado en el portapapeles!
@WebResult annotation is defined by the javax.jws.WebResult interface. It is placed on the methods defined in the SEI. The @WebResult annotation allows you to specify the properties of the wsdl:part that is generated for the method's return value.
@WebResult Properties” describes the properties of the @WebResult annotation.
Example Copiar enlaceEnlace copiado en el portapapeles!
Example 23.7. Fully Annotated SEI
23.2.3. Apache CXF Annotations Copiar enlaceEnlace copiado en el portapapeles!
23.2.3.1. WSDL Documentation Copiar enlaceEnlace copiado en el portapapeles!
@WSDLDocumentation annotation Copiar enlaceEnlace copiado en el portapapeles!
@WSDLDocumentation annotation is defined by the org.apache.cxf.annotations.WSDLDocumentation interface. It can be placed on the SEI or the SEI methods.
wsdl:documentation elements after the SEI is converted to WSDL. By default, the documentation elements appear inside the port type, but you can specify the placement property to make the documentation appear at other locations in the WSDL file. Table 23.9, “@WSDLDocumentation properties” shows the properties supported by the @WSDLDocumentation annotation.
| Property | Description |
|---|---|
value | (Required) A string containing the documentation text. |
placement | (Optional) Specifies where in the WSDL file this documentation is to appear. For the list of possible placement values, see the section called “Placement in the WSDL contract”. |
faultClass | (Optional) If the placement is set to be FAULT_MESSAGE, PORT_TYPE_OPERATION_FAULT, or BINDING_OPERATION_FAULT, you must also set this property to the Java class that represents the fault. |
@WSDLDocumentationCollection annotation Copiar enlaceEnlace copiado en el portapapeles!
@WSDLDocumentationCollection annotation is defined by the org.apache.cxf.annotations.WSDLDocumentationCollection interface. It can be placed on the SEI or the SEI methods.
Placement in the WSDL contract Copiar enlaceEnlace copiado en el portapapeles!
placement property, which is of type WSDLDocumentation.Placement. The placement can have one of the following values:
WSDLDocumentation.Placement.BINDINGWSDLDocumentation.Placement.BINDING_OPERATIONWSDLDocumentation.Placement.BINDING_OPERATION_FAULTWSDLDocumentation.Placement.BINDING_OPERATION_INPUTWSDLDocumentation.Placement.BINDING_OPERATION_OUTPUTWSDLDocumentation.Placement.DEFAULTWSDLDocumentation.Placement.FAULT_MESSAGEWSDLDocumentation.Placement.INPUT_MESSAGEWSDLDocumentation.Placement.OUTPUT_MESSAGEWSDLDocumentation.Placement.PORT_TYPEWSDLDocumentation.Placement.PORT_TYPE_OPERATIONWSDLDocumentation.Placement.PORT_TYPE_OPERATION_FAULTWSDLDocumentation.Placement.PORT_TYPE_OPERATION_INPUTWSDLDocumentation.Placement.PORT_TYPE_OPERATION_OUTPUTWSDLDocumentation.Placement.SERVICEWSDLDocumentation.Placement.SERVICE_PORTWSDLDocumentation.Placement.TOP
Example of @WSDLDocumentation Copiar enlaceEnlace copiado en el portapapeles!
@WSDLDocumentation annotation to the SEI and to one of its methods.
Example 23.8. Using @WSDLDocumentation
documentation elements are, respectively, PORT_TYPE and PORT_TYPE_OPERATION.
Example 23.9. WSDL generated with documentation
Example of @WSDLDocumentationCollection Copiar enlaceEnlace copiado en el portapapeles!
@WSDLDocumentationCollection annotation to an SEI.
Example 23.10. Using @WSDLDocumentationCollection
23.2.3.2. Schema Validation of Messages Copiar enlaceEnlace copiado en el portapapeles!
@SchemaValidation annotation Copiar enlaceEnlace copiado en el portapapeles!
@SchemaValidation annotation is defined by the org.apache.cxf.annotations.SchemaValidation interface. It is placed on the SEI.
Example Copiar enlaceEnlace copiado en el portapapeles!
HelloWorld SEI.
Example 23.11. Activating schema validation
@WebService
@SchemaValidation
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
@WebService
@SchemaValidation
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
23.2.3.3. Specifying the Data Binding Copiar enlaceEnlace copiado en el portapapeles!
@DataBinding annotation Copiar enlaceEnlace copiado en el portapapeles!
@DataBinding annotation is defined by the org.apache.cxf.annotations.DataBinding interface. It is placed on the SEI.
@DataBinding annotation must be the class that provides the data binding, ClassName.class.
Supported data bindings Copiar enlaceEnlace copiado en el portapapeles!
org.apache.cxf.jaxb.JAXBDataBinding(Default) The standard JAXB data binding.org.apache.cxf.sdo.SDODataBindingThe Service Data Objects (SDO) data binding is based on the Apache Tuscany SDO implementation. If you want to use this data binding in the context of a Maven build, you need to add a dependency on thecxf-rt-databinding-sdoartifact.org.apache.cxf.aegis.databinding.AegisDatabindingIf you want to use this data binding in the context of a Maven build, you need to add a dependency on thecxf-rt-databinding-aegisartifact.org.apache.cxf.xmlbeans.XmlBeansDataBindingIf you want to use this data binding in the context of a Maven build, you need to add a dependency on thecxf-rt-databinding-xmlbeansartifact.org.apache.cxf.databinding.source.SourceDataBindingThis data binding belongs to the Apache CXF core.org.apache.cxf.databinding.stax.StaxDataBindingThis data binding belongs to the Apache CXF core.
Example Copiar enlaceEnlace copiado en el portapapeles!
HelloWorld SEI
Example 23.12. Setting the data binding
@WebService
@DataBinding(org.apache.cxf.sdo.SDODataBinding.class)
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
@WebService
@DataBinding(org.apache.cxf.sdo.SDODataBinding.class)
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
23.2.3.4. Compressing Messages Copiar enlaceEnlace copiado en el portapapeles!
@GZIP annotation Copiar enlaceEnlace copiado en el portapapeles!
@GZIP annotation is defined by the org.apache.cxf.annotations.GZIP interface. It is placed on the SEI.
Accept header will be added and, if the server supports GZIP compression, the response will be gzipped and any subsequent requests will be also.
@GZIP annotation.
| Property | Description |
|---|---|
threshold | Messages smaller than the size specified by this property are not gzipped. Default is -1 (no limit). |
@FastInfoset Copiar enlaceEnlace copiado en el portapapeles!
@FastInfoset annotation is defined by the org.apache.cxf.annotations.FastInfoset interface. It is placed on the SEI.
Accept header will be added and, if the server supports FastInfoset, the response will be in FastInfoset and any subsequent requests will be also.
@FastInfoset annotation.
| Property | Description |
|---|---|
force | A boolean property that forces the use of FastInfoset format, instead of negotiating. When true, force the use of FastInfoset format; otherwise, negotiate. Default is false. |
Example of @GZIP Copiar enlaceEnlace copiado en el portapapeles!
HelloWorld SEI.
Example 23.13. Enabling GZIP
@WebService
@GZIP
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
@WebService
@GZIP
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
Exampe of @FastInfoset Copiar enlaceEnlace copiado en el portapapeles!
HelloWorld SEI.
Example 23.14. Enabling FastInfoset
@WebService
@FastInfoset
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
@WebService
@FastInfoset
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
23.2.3.5. Enable Logging on an Endpoint Copiar enlaceEnlace copiado en el portapapeles!
@Logging annotation Copiar enlaceEnlace copiado en el portapapeles!
@Logging annotation is defined by the org.apache.cxf.annotations.Logging interface. It is placed on the SEI.
| Property | Description |
|---|---|
limit | Specifies the size limit, beyond which the message is truncated in the logs. Default is 64K. |
inLocation | Specifies the location to log incoming messages. Can be either <stderr>, <stdout>, <logger>, or a filename. Default is <logger>. |
outLocation | Specifies the location to log outgoing messages. Can be either <stderr>, <stdout>, <logger>, or a filename. Default is <logger>. |
Example Copiar enlaceEnlace copiado en el portapapeles!
HelloWorld SEI, where incoming messages are sent to <stdout> and outgoing messages are sent to <logger>.
Example 23.15. Logging configuration using annotations
@WebService
@Logging(limit=16000, inLocation="<stdout>")
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
@WebService
@Logging(limit=16000, inLocation="<stdout>")
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
23.2.3.6. Adding Properties and Policies to an Endpoint Copiar enlaceEnlace copiado en el portapapeles!
wsdl:policy elements that appear in the WSDL contract. By contrast, properties are Apache CXF-specific and they are normally set by defining jaxws:properties elements in the Apache CXF Spring configuration file.
23.2.3.6.1. Adding properties Copiar enlaceEnlace copiado en el portapapeles!
@EndpointProperty annotation Copiar enlaceEnlace copiado en el portapapeles!
@EndpointProperty annotation is defined by the org.apache.cxf.annotations.EndpointProperty interface. It is placed on the SEI.
jaxws:properties element in a Spring configuration file as follows:
@EndpointProperty annotations to the SEI, as shown in Example 23.16, “Configuring WS-Security Using @EndpointProperty Annotations”.
Example 23.16. Configuring WS-Security Using @EndpointProperty Annotations
@EndpointProperties annotation Copiar enlaceEnlace copiado en el portapapeles!
@EndpointProperties annotation is defined by the org.apache.cxf.annotations.EndpointProperties interface. It is placed on the SEI.
@EndpointProperty annotations into a list. Using @EndpointProperties, it is possible to re-write Example 23.16, “Configuring WS-Security Using @EndpointProperty Annotations” as shown in Example 23.17, “Configuring WS-Security Using an @EndpointProperties Annotation”.
Example 23.17. Configuring WS-Security Using an @EndpointProperties Annotation
23.2.3.6.2. Adding policies Copiar enlaceEnlace copiado en el portapapeles!
@Policy annotation Copiar enlaceEnlace copiado en el portapapeles!
@Policy annotation is defined by the org.apache.cxf.annotations.Policy interface. It can be placed on the SEI or the SEI methods.
wsdl:policy element. If a WSDL contract is to be generated from the SEI (for example, using the java2ws command-line tool), you can specify whether or not you want to include this policy in the WSDL.
@Policy annotation.
| Property | Description |
|---|---|
uri | (Required) The location of the file containing the policy definition. |
includeInWSDL | (Optional) Whether to include the policy in the generated contract, when generating WSDL. Default is true. |
placement | (Optional) Specifies where in the WSDL file this documentation is to appear. For the list of possible placement values, see the section called “Placement in the WSDL contract”. |
faultClass | (Optional) If the placement is set to be BINDING_OPERATION_FAULT or PORT_TYPE_OPERATION_FAULT, you must also set this property to specify which fault this policy applies to. The value is the Java class that represents the fault. |
@Policies annotation Copiar enlaceEnlace copiado en el portapapeles!
@Policies annotation is defined by the org.apache.cxf.annotations.Policies interface. It can be placed on the SEI or thse SEI methods.
@Policy annotations into a list.
Placement in the WSDL contract Copiar enlaceEnlace copiado en el portapapeles!
placement property, which is of type Policy.Placement. The placement can have one of the following values:
Example of @Policy Copiar enlaceEnlace copiado en el portapapeles!
HelloWorld SEI and how to associate a policy with the sayHi method. The policies themselves are stored in XML files in the file system, under the annotationpolicies directory.
Example of @Policies Copiar enlaceEnlace copiado en el portapapeles!
@Policies annotation to group multiple @Policy annotations into a list, as shown in the following example: