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.此内容没有您所选择的语言版本。
Chapter 4. Deploying an Apache CXF Web Service
Abstract
This tutorial describes how to deploy an Apache CXF Web services endpoint in a WAR file, where the Web service endpoint is implemented by binding directly to a Java class with the JAX-WS mapping.
4.1. Apache CXF Example 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Overview 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Figure 4.1, “Example Web Service Deployed in a Web Server” gives an overview of the Apache CXF example deployed in a Web server, which lets you see how the Web service's URL is constructed from settings at different configuration layers. The Web server's host and port, the WAR file name, the
url-pattern setting from web.xml, and the address attribute of the Web services endpoint are combined to give the URL, http://localhost:8080/wsdl_first/services/CustomerServicePort.
Figure 4.1. Example Web Service Deployed in a Web Server
wsdl_first sample 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The code for this example is available from the standard Apache CXF distribution, under the
samples/wsdl_first directory. For details of how to install the Apache CXF distribution, see the section called “Install Apache CXF”.
web.xml file 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To deploy the example Web service as a servlet, you must provide a properly configured
web.xml file. In the wsdl_first project, the web.xml file is stored at the following location:
wsdl_first/src/main/webapp/WEB-INF/web.xml
wsdl_first/src/main/webapp/WEB-INF/web.xml
Example 4.1, “web.xml File for the wsdl_first Example” shows the contents of the
web.xml file.
Example 4.1. web.xml File for the wsdl_first Example
The key settings in the preceding
web.xml file are:
- Servlet class—specifies the
org.apache.cxf.transport.servlet.CXFServletclass, which implements a special servlet that integrates with Web services. - URL pattern—determines which URLs are routed to this servlet. In general, the servlet URL has the following form:
http://Host:Port/WARFileName/URLPattern
http://Host:Port/WARFileName/URLPatternCopy to Clipboard Copied! Toggle word wrap Toggle overflow Where the base URL,http://Host:Port, is determined by the configuration of the Web server, theWARFileNameis the root of theWARFileName.warWAR file, and theURLPatternis specified by the contents of theurl-patternelement.Assuming that the Web server port is set to 8080, thewsdl_firstexample servlet will match URLs of the following form:http://localhost:8080/wsdl_first/services/*
http://localhost:8080/wsdl_first/services/*Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Implied Spring container 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The
CXFServlet automatically creates and starts up a Spring container, which you can then use for defining Web service endpoints. By default, this Spring container automatically loads the following XML file in the WAR:
WEB-INF/cxf-servlet.xml
WEB-INF/cxf-servlet.xml
In the
wsdl_first example project, this file is stored at the following location:
wsdl_first/src/main/webapp/WEB-INF/cxf-servlet.xml
wsdl_first/src/main/webapp/WEB-INF/cxf-servlet.xml
cxf-servlet.xml file 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The
cxf-servlet.xml file is primarily used to create Web service endpoints, which represent the Web services exposed through the Web server. Apache CXF provides a convenient and flexible syntax for defining Web service endpoints in XML and you can use this flexible syntax to define endpoints in cxf-servlet.xml.
Example 4.2, “Spring Configuration for the wsdl_first Example” shows the contents of the
cxf-servlet.xml file, which creates a single CustomerService endpoint, using the jaxws:endpoint element.
Example 4.2. Spring Configuration for the wsdl_first Example
Note that the
address attribute of the jaxws:endpoint specifies the final segment of the Web service's URL. When you put together all of the settings from the Web server, the web.xml file, and the cxf-server.xml file, you obtain the following URL for this Web service endpoint:
http://localhost:8080/wsdl_first/services/CustomerServicePort
http://localhost:8080/wsdl_first/services/CustomerServicePort
WSDL address configuration 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
In addition to defining the servlet descriptor,
web.xml, and the Spring configuration, cxf-servlet.xml, it is also necessary to ensure that the SOAP address in the WSDL contract is correctly specified, so that it matches the URL for this Web service.
In the
wsdl_first example, the WSDL contract is located in the following file:
wsdl_first/src/main/resources/CustomerService.wsdl
wsdl_first/src/main/resources/CustomerService.wsdl
In the WSDL contract, the
location attribute of the soap:address element must be set to the correct Web service URL, as shown in Example 4.3, “Address in the WSDL CustomerService Contract”.
Example 4.3. Address in the WSDL CustomerService Contract