32.4. Instantiate the WS Endpoint
Overview
In Apache CXF, you create a WS endpoint by defining a
jaxws:endpoint
element in XML. The WS endpoint is effectively the runtime representation of the Web service: it opens an IP port to listen for SOAP/HTTP requests, is responsible for marshalling and unmarshalling messages (making use of the generated Java stub code), and routes incoming requests to the relevant methods on the implementor class.
In other words, creating a Web service in Spring XML consists essentially of the following two steps:
- Create an instance of the implementor class, using the Spring
bean
element. - Create a WS endpoint, using the
jaxws:endpoint
element.
The jaxws:endpoint element
You can instantiate a WS endpoint using the
jaxws:endpoint
element in a Spring file, where the jaxws:
prefix is associated with the http://cxf.apache.org/jaxws
namespace.
Note
Take care not to confuse the
jaxws:endpoint
element with the cxf:cxfEndpoint
element, which you meet later in this guide: the jaxws:endpoint
element is used to integrate a WS endpoint with a Java implementation class; whereas the cxf:cxfEndpoint
is used to integrate a WS endpoint with a Camel route.
Define JAX-WS endpoint in XML
The following sample Spring file shows how to define a JAX-WS endpoint in XML, using the
jaxws:endpoint
element.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:endpoint xmlns:customer="http://demo.fusesource.com/wsdl/CustomerService/" id="customerService" address="/Customer" serviceName="customer:CustomerService" endpointName="customer:SOAPOverHTTP" implementor="#customerServiceImpl"> </jaxws:endpoint> <bean id="customerServiceImpl" class="com.fusesource.customer.ws.CustomerServiceImpl"/> </beans>
Address for the Jetty container
Apache CXF deploys the WS endpoint into a Jetty servlet container instance and the
address
attribute of jaxws:endpoint
is therefore used to configure the addressing information for the endpoint in the Jetty container.
Apache CXF supports the notion of a default servlet container instance. The way the default servlet container is initialized and configured depends on the particular mode of deployment that you choose. For example the Red Hat JBoss Fuse container and Web containers (such as Tomcat) provide a default servlet container.
There are two different syntaxes you can use for the endpoint address, where the syntax that you use effectively determines whether or not the endpoint is deployed into the default servlet container, as follows:
- Address syntax for default servlet container—to use the default servlet container, specify only the servlet context for this endpoint. Do not specify the protocol, host, and IP port in the address. For example, to deploy the endpoint to the
/Customers
servlet context in the default servlet container:address="/Customers"
- Address syntax for custom servlet container—to instantiate a custom Jetty container for the endpoint, specify a complete HTTP URL, including the host and IP port (the value of the IP port effectively identifies the target Jetty container). Typically, for a Jetty container, you specify the host as
0.0.0.0
, which is interpreted as a wildcard that matches every IP network interface on the local machine (that is, if deployed on a multi-homed host, Jetty opens a listening port on every network card). For example, to deploy the endpoint to the custom Jetty container listening on IP port,8083
:address="http://0.0.0.0:8083/Customers"
NoteIf you want to configure a secure endpoint (secured by SSL), you would specify thehttps:
scheme in the address.
Referencing the service implementation
The
implementor
attribute of the jaxws:endpoint
element references the implementation of the WS service. The value of this attribute can either be the name of the implementation class or (as in this example) a bean reference in the format, #BeanID
, where the #
character indicates that the following identifier is the name of a bean in the bean registry.