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.Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
23.2. Creating a Service Object
Overview Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
The
javax.xml.ws.Service class represents the wsdl:service element which contains the definition of all of the endpoints that expose a service. As such, it provides methods that allow you to get endpoints, defined by wsdl:port elements, that are proxies for making remote invocations on a service.
Note
The
Service class provides the abstractions that allow the client code to work with Java types as opposed to working with XML documents.
The create() methods Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
The
Service class has two static create() methods that can be used to create a new Service object. As shown in Example 23.1, “Service create() Methods”, both of the create() methods take the QName of the wsdl:service element the Service object will represent, and one takes a URI specifying the location of the WSDL contract.
Note
All services publish their WSDL contracts. For SOAP/HTTP services the URI is usually the URI for the service appended with
?wsdl.
Example 23.1. Service create() Methods
public static Service create(URL wsdlLocation,
QName serviceName)
throws WebServiceException;public static Service create(QName serviceName)
throws WebServiceException;
The value of the
serviceName parameter is a QName. The value of its namespace part is the target namespace of the service. The service's target namespace is specified in the targetNamespace property of the @WebService annotation. The value of the QName's local part is the value of wsdl:service element's name attribute. You can determine this value in one of the following ways:
- It is specified in the serviceName property of the
@WebServiceannotation. - You append
Serviceto the value of the name property of the@WebServiceannotation. - You append
Serviceto the name of the SEI.
Important
Programmatically-created CXF consumers deployed in OSGi environments require special handling to avoid the likelihood of incurring
ClassNotFoundExceptions. For each bundle that contains programmatically-created CXF consumers, you need to create a singleton CXF default bus and ensure that all of the bundle's CXF consumers use it. Without this safeguard, one bundle could be assigned the CXF default bus created in another bundle, which could cause the inheriting bundle to fail.
For example, suppose bundle A did not explicitly set a CXF default bus and was assigned the CXF default bus created in bundle B. If the CXF bus in bundle A needed to be configured with additional features (such as SSL or WS-Security) or needed to load certain classes or resources from the application in bundle A, it would fail. This is so because the CXF bus instance sets a thread context class loader (TCCL) as the bundle class loader of the bundle that created it (in this case bundle B). Furthermore, certain frameworks, such as wss4j (implements WS-Security in CXF) use the TCCL to load resources, such as calback handler classes or other property files, from inside the bundle. Because bundle A is assigned bundle B's default CXF bus and it's TCCL, the wss4j layer cannot load the required resources from bundle A, which results in
ClassNotFoundException errors.
To create the singleton CXF default bus, insert this code:
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
at the beginning of the
BusFactory.setThreadDefaultBus(BusFactory.newInstance().createBus());
BusFactory.setThreadDefaultBus(BusFactory.newInstance().createBus());
main method that creates the service object, as shown in the section called “Example”.
Example Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
Example 23.2, “Creating a
Service Object” shows code for creating a Service object for the SEI shown in Example 22.7, “Fully Annotated SEI”.
Example 23.2. Creating a Service Object
The code in Example 23.2, “Creating a
Service Object” does the following:
- 1
- Creates a singleton CXF default bus that is available to all CXF consumers of the service.
- 2
- Builds the QName for the service using the targetNamespace property and the name property of the
@WebServiceannotation. - 3
- Calls the single parameter
create()method to create a newServiceobject.NoteUsing the single parametercreate()frees you from having any dependencies on accessing a WSDL contract.