이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 3. 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.

3.1. Apache CXF Example

Overview

Figure 3.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 3.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
Copy to Clipboard Toggle word wrap
Example 3.1, “web.xml File for the wsdl_first Example” shows the contents of the web.xml file.

Example 3.1. web.xml File for the wsdl_first Example

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>cxf</display-name>

    <servlet>
        <servlet-name>cxf</servlet-name>
        <display-name>cxf</display-name>
        <description>Apache CXF Endpoint</description>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

</web-app>
Copy to Clipboard Toggle word wrap
The key settings in the preceding web.xml file are:
  • Servlet class—specifies the org.apache.cxf.transport.servlet.CXFServlet class, 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
    Copy to Clipboard Toggle word wrap
    Where the base URL, http://Host:Port, is determined by the configuration of the Web server, the WARFileName is the root of the WARFileName.war WAR file, and the URLPattern is specified by the contents of the url-pattern element.
    Assuming that the Web server port is set to 8080, the wsdl_first example servlet will match URLs of the following form:
    http://localhost:8080/wsdl_first/services/*
    Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap
In the wsdl_first example project, this file is stored at the following location:
wsdl_first/src/main/webapp/WEB-INF/cxf-servlet.xml
Copy to Clipboard Toggle word wrap

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 3.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 3.2. Spring Configuration for the wsdl_first Example

<?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://customerservice.example.com/"
            id="CustomerServiceHTTP"
            address="/CustomerServicePort"
            serviceName="customer:CustomerServiceService"
            endpointName="customer:CustomerServicePort"
            implementor="com.example.customerservice.server.CustomerServiceImpl">
	<!--jaxws:properties>
	    <entry key="schema-validation-enabled" value="true" />
	</jaxws:properties-->
    </jaxws:endpoint>

</beans>
Copy to Clipboard Toggle word wrap
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
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap
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 3.3, “Address in the WSDL CustomerService Contract”.

Example 3.3. Address in the WSDL CustomerService Contract

<?xml version="1.0" encoding="UTF-8"?>
...
<wsdl:definitions name="CustomerServiceService" targetNamespace="http://customerservice.example.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://customerservice.example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  ...
  <wsdl:service name="CustomerServiceService">
    <wsdl:port name="CustomerServicePort" binding="tns:CustomerServiceServiceSoapBinding">
      <!-- embedded deployment -->
      <!--soap:address location="http://localhost:9090/CustomerServicePort"/-->
      <!-- standalone Tomcat deployment -->
      <soap:address location="http://localhost:8080/wsdl_first/services/CustomerServicePort"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat