Welcome!

Apache Authors: Elizabeth White, Pat Romanski, Liz McMillan, Christopher Harrold, Janakiram MSV

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) View Comments

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.


Most Recent Comments
kuldeep_saini83 08/29/09 08:33:00 PM EDT

Hi,
I was searching some article which can help me to implement web service using contract first approach.
I found this article very useful and informative.
Just one thing, code snippets should be complete so that any one can just take that code and able to run WB services.
Still it helped me lot and finally i was able to run WB by contract first approach.

Thaanks...!!!

yogesh 10/06/08 02:27:38 PM EDT

Hey Yang,
Nice article. But how actually we handle transaction in case of spring and cxf?

IoT & Smart Cities Stories
The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and smart thermostats, are fairly well-understood. However, there are a new set of challenges for IoT devices that generate megabytes or gigabytes of data per second. Certainly, the infrastructure will have to change, as those volumes of data will likely overwhelm the available bandwidth for aggregating the data into a central repository. Ochandarena discusses a whole new way to think about your next...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
All in Mobile is a place where we continually maximize their impact by fostering understanding, empathy, insights, creativity and joy. They believe that a truly useful and desirable mobile app doesn't need the brightest idea or the most advanced technology. A great product begins with understanding people. It's easy to think that customers will love your app, but can you justify it? They make sure your final app is something that users truly want and need. The only way to do this is by ...
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
DXWorldEXPO LLC announced today that Big Data Federation to Exhibit at the 22nd International CloudEXPO, colocated with DevOpsSUMMIT and DXWorldEXPO, November 12-13, 2018 in New York City. Big Data Federation, Inc. develops and applies artificial intelligence to predict financial and economic events that matter. The company uncovers patterns and precise drivers of performance and outcomes with the aid of machine-learning algorithms, big data, and fundamental analysis. Their products are deployed...
Cell networks have the advantage of long-range communications, reaching an estimated 90% of the world. But cell networks such as 2G, 3G and LTE consume lots of power and were designed for connecting people. They are not optimized for low- or battery-powered devices or for IoT applications with infrequently transmitted data. Cell IoT modules that support narrow-band IoT and 4G cell networks will enable cell connectivity, device management, and app enablement for low-power wide-area network IoT. B...
The hierarchical architecture that distributes "compute" within the network specially at the edge can enable new services by harnessing emerging technologies. But Edge-Compute comes at increased cost that needs to be managed and potentially augmented by creative architecture solutions as there will always a catching-up with the capacity demands. Processing power in smartphones has enhanced YoY and there is increasingly spare compute capacity that can be potentially pooled. Uber has successfully ...
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...