Welcome!

Apache Authors: Carmen Gonzalez, Liz McMillan, Elizabeth White, Pat Romanski, Christopher Harrold

Related Topics: ColdFusion, Apache

ColdFusion: Article

Web Services Using ColdFusion and Apache CXF

Enabling Web Services through Java

The core part of this snippet is the java task with the classname attribute set to org.apache.cxf.tools.wsdlto.WSDLToJava. Include this macro in your build file and then you can call it as shown in Listing 7.

Successfully running the tool will generate all necessary software artifacts, which include a service interface, a service endpoint interface (SEI), Java classes that implement all types defined in the WSDL document, and APIs that are used to obtain instances of these types.

Service Implementation Class
Now it's time to provide an implementation for the service. We do so by creating a Java class called MovieServiceImpl. There are some requirements that this class has to fulfill to become a service implementation class. It must be annotated with the JAX-WS @WebService annotation, and the endpointInterface member attribute of the annotation must refer to the generated SEI, i.e., org.example.movieservice.MovieService in our example. The service implementation bean may implement the SEI, but it's not required to do so. The service implementation bean must implement all method signatures defined in the SEI.

Listing 8 shows a code snippet of the implementation class.

Server Development and Service Publishing
We now need to create a server that hosts our service. XML-based Java Web Services can be hosted either in a managed or in non-managed environment.

Non-Managed Environment
JAX-WS provides functionality for creating and publishing Web Service endpoints dynamically using javax.xml.ws.Endpoint API running in a non-managed environment. CXF is JAX-WS-compliant and so supports JAX-WS dynamic Web Service publishing. Listing 9 is a code snippet of dynamic publishing.

This code is not CXF-specific and is portable among all JAX-WS-compliant runtimes. The complete server code is straightforward and sp omitted here. Start the server and you can then test the server by pointing your browser to the location: http://localhost:8080/MyMovieService?wsdl.

Managed Environment
Implementing Enterprise Web Services (JSR 109) defines ways of implementing Web Services and publishing endpoints in a managed environment. The use of dynamic publishing is considered non-portable in a managed environment. So JSR 109 requires that both the servlet and EJB container disallow publishing the endpoint dynamically by not granting the publishEndpoint security permission.

There are two means of implementing Web Services that run in a managed Java EE environment. The first is a container-based extension of the JAX-RPC or JAX-WS programming model, which defines a Web Service as a Java class running in Web container. The second one defines the use of a stateless enterprise session bean to run in the EJB container.

Due to the fact that CXF doesn't come with any Java EE container, neither is it specifically designed to support a particular vendor's Java EE container, it's quite different to implement Web Services in a managed environment using CXF. First, CXF doesn't support deploying Web Services in an EJB container. Second, the way of deploying Web Services in a web container is different from the one defined by JSR 109. While JSR 109 requires the container runtime to embed each endpoint implementation class in one servlet, CXF provides a generic servlet for all endpoint implementation classes.

Let's see this in action by deploying our example service to a Web container. The whole process involves 1.) configuring the Spring runtime to be aware of the service implementation class, 2.) registering the CXF servlet with the Web container and lastly, 3.) providing a configuration file that glues the last two together.

Configuring Spring is ;ole configuring a Spring project. A Spring context loader listener is added to the Web container via web.xml. Also added is the Spring configuration file, cxf-bean.xml, which is passed to the container as a context parameter. We'll cover this file momentarily. Listing 10 is a fragment of the web.xml file.

Registering CXF servlet is also straightforward. See Listing 11. Note that only one servlet is needed, and it acts as the entry point of all service endpoints.

We now need to somehow let the servlet know how to dispatch requests to the corresponding service implementation beans. This is the exact purpose of cxf-bean.xml, which is listed in Listing 12.

Using this file, we guide the servlet to forward all requests at the address /MovieService to MovieServiceImpl bean. Note that you'd better make sure that the value of the address attribute matches the one you advertised in the WSDL document.

After packaging the application and deploying it to a Web container (say, Tomcat), we can test the service by pointing the browser to the address http://localhost:8080/MovieService/cxf/MovieService?wsdl.

This concludes the server development. Now we move on to client development.

Client Development
In general, JAX-WS defines the Web Service client programming model, while JSR 109 defines the Web Service client programming model in a Java EE environment.

As mentioned, CXF doesn't have any Java EE runtime, so the primary way of creating a client is through JAX-WS. Listing 13 shows a simple client that consumes the MovieService Web Service.

RESTful Web Services Support
CXF supports RESTful Web Services by annotations. It uses four annotations for specifying the HTTP verb that will be used for a method:

  • org.codehaus.jra.Delete specifies that the method maps to a DELETE.
  • org.codehaus.jra.Get specifies that the method maps to a GET.
  • org.codehaus.jra.Post specifies that the method maps to a POST.
  • org.codehaus.jra.Put specifies that the method maps to a PUT.

Listing 14 is a code snippet that shows how annotations are used to implement RESTful services. Four services are defined that let clients book tickets, get ticket information, modify the booking, and cancel the booking, respectively. The code itself is fairly self-explanatory.

Assuming that these services are deployed and running, a client system can modify booking information by sending an HTTP request to an URL similar to the following, with the METHOD attribute of the request set to PUT. Note the bold portion of the URL:

http://www.mymovieticket.com/ticketbooking/tickets/9182


More Stories By Jinsong Yang

Jinsong Yang is a senior application engineer at Warnerbros. Advanced Digital Services, and is a Sun Certified Enterprise Architect. He has devoted the last six-plus years to designing and integrating large-scale Java EE applications. He holds an MS in computer science from UCLA.

Comments (3)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
Moroccanoil®, the global leader in oil-infused beauty, is thrilled to announce the NEW Moroccanoil Color Depositing Masks, a collection of dual-benefit hair masks that deposit pure pigments while providing the treatment benefits of a deep conditioning mask. The collection consists of seven curated shades for commitment-free, beautifully-colored hair that looks and feels healthy.
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
We all love the many benefits of natural plant oils, used as a deap treatment before shampooing, at home or at the beach, but is there an all-in-one solution for everyday intensive nutrition and modern styling?I am passionate about the benefits of natural extracts with tried-and-tested results, which I have used to develop my own brand (lemon for its acid ph, wheat germ for its fortifying action…). I wanted a product which combined caring and styling effects, and which could be used after shampo...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.