Apache CXF Web Service DevelopmentDevelop and deploy SOAP and RESTful Web No part of this book may be reproduced, stored in a retrieval system. Apache CXF Web Service Development Develop and deploy SOAP and Developing Web Services with Apache CXF and Axis2 5 Foreword Learn web serv. JAX-WS , JAX-RS , (TCK tests). ▫ Basic support: WS-I Basic Profile. ▫ Metadata: WS-Policy, WSDL ▫ Messaging: WS-Addressing, SOAP /

Apache Cxf Web Service Book Pdf

Language:English, Indonesian, Dutch
Genre:Politics & Laws
Published (Last):04.01.2016
ePub File Size:27.31 MB
PDF File Size:18.52 MB
Distribution:Free* [*Registration Required]
Uploaded by: KEILA

Download Citation on ResearchGate | Apache CXF Web Service This is the first book that gives details on how to use the Apache CXF framework for. 2 In this package, you will find: A Biography of the authors of the book A preview chapter from the book, Chapter NO.2 "Developing a Web Service with CXF" A. Chapter 5 – Apache CXF and Tomcat Server. - Chapter 6 Figure Deployment diagram for CXF Web Service application and Oracle WebLogic server.

He has over nine years of industrial experience and has architected and implemented large scale enterprise solutions. I would also like to thank my parents for their support and encouragement in all my endeavors. And last but not least, to my good friend and co-author Rajeev Hathi. His hobbies are watching sports and listening to rock music. His favorite bands are Pink Floyd and Dire Straits. Rajeev has written several articles for IBM developerWorks portal. Rajeev has been working on J2EE-based projects for more than ten years now.

He has worked with several companies offering software services and conducted various knowledge sessions on Java and J2EE. A book is often the product of many hands.

To start with I'd like to thank Usha Iyer, an Acquisition Editor with Packt Publishing, for having enough faith in my writing skills and abilities.. Developing a client In the previous section we discussed and illustrated how to develop and publish a web service. We now have the server-side code that publishes our OrderProcess web service. The next set of tasks will be to create the client-side code that will consume or invoke our OrderProcess web service.

To achieve this, we will perform the following steps: Develop the client-beans. The following code illustrates the client-beans. In this case, jaxws:client is a bean and the id name is orderclient. The bean will represent an SEI. The client application will make use of this SEI to invoke the web service.

Developing web service client code We will now create a standalone Java class to invoke our OrderProcess web service. The context component's getbean method is passed the bean ID orderclient. Using the SEI, we then invoke the web service method processorder.

One thing to observe here is that the client always uses the interface to invoke a web service method. The processorder method takes the Order bean as a parameter. The method will then process the order and return the unique order ID. To summarize, we created the OrderProcess service endpoint interface and the implementation class.

We then created server and client-side Spring-based configuration files and finally we created the client application. The relevant components are developed and we are all set to run or execute our code.

But before we do that, you will have to create one final component that will integrate Spring and CXF. We need to wire Spring and CXF through web. The following code illustrates the web.

The web. It defines the listener class ContextLoaderListener, which is responsible for loading the server-side configuration file beans.

So upon the web server startup, the order process web service endpoint will be registered and published. Before running the program, we will organize the code so far developed in the appropriate folder structure. You can create the folder structure, as shown in the following screenshot, and put the components in the respective sub folders The developed code will go into the following: The Java code will go into the respective package folders The beans. It will typically involve three steps: Building the code Deploying the code Executing the code [ 36 ] 20 Building the code Building the code means compiling the source Java code.

We will use the ANT tool to do this. The following code illustrates the sample build. Please make sure that you have set up the environment variables as mentioned in Appendix A.

It will build the code and put the class files under the newly created build folder. The following figure shows the output generated upon running the ant command. Deployment effectively means building and moving the code archive to the server deploy path.

We will be using the Tomcat web container to deploy and run the application. To deploy our built code, navigate to project root folder, and enter the following command: ant deploy This will build the WAR file and put it under the Tomcat server webapp path.

Executing the code Following code deployment, we are all set to run the Order Process Application.

You will execute the Java client program Client. The program will invoke the processorder method that will generate the order ID if the specified order is approved.

Before running the client program, we need to start the Tomcat web server. There are several ways of starting the Tomcat server depending on the Tomcat version that is installed.

Once the server is started, you need to run the client program by giving the following command at the command prompt window: ant client As you can see above, we are using Ant to run the client program.

Quick Start

Upon executing this command, it will generate the following output: Thus we have successfully executed the order processing web service. The CXF bus is comprised of a Spring-based configuration file, namely, cxf. It defines a common context for all the endpoints. It wires all the runtime infrastructure components and provides a common application context.

Apache CXF Web Service Development(1).pdf

Now, we know that CXF internally uses Spring for its configuration. The following XML fragment shows the bus definition in the cxf. The class acts more as an interceptor provider for incoming and outgoing requests to a web service endpoint.

These interceptors, once defined, are available to all the endpoints in that context. The cxf.

These components are made available as bus extensions. One can access these infrastructure objects using the getextension method. These infrastructure components are registered so as to get and update various service endpoint level parameters such as service binding, transport protocol, conduits, and so on.

CXF bus architecture can be overridden, but one must apply caution when overriding the default bus behavior. Since the bus is the core component that loads the CXF runtime, many shared objects are also loaded as part of this runtime.

Apache CXF Web Service Development

You want to make sure that these objects are loaded when overriding the existing bus implementation. You can extend the default bus to include your own custom components or service objects such as factory managers. You can also add interceptors to the bus bean.

These interceptors defined at the bus level are available to all the endpoints.

The following code shows how to create a custom bus: SpringBeanFactory. You can complement or overwrite the bean definitions that the original cxf. For the CXF to load the mycxf. Frontend CXF provides the concept of frontend modeling, which lets you create web services using different frontend APIs. It also lets you create dynamic web service clients. We will look at how to use the Frontend programming model in the next chapter. JAX-WS simplifies web service development. It defines Java-based APIs that ease the development and deployment of web services.

The specification supports WS-Basic Profile 1. It effectively means a web service can be invoked or consumed by a client written in any language. JAX-WS also speeds up web service development by providing a library of annotations to turn Plain Old Java classes into web services and specifies a detailed mapping from a service defined in WSDL to the Java classes that will implement that service.

As discussed earlier, two approaches for web service development exist: Code-First and Contract-First.

Related titles

With JAX-WS, you can perform web service development using one of the said approaches, depending on the nature of the application. With the Code-first approach, you start by developing a Java class and interface and annotating the same as a web service. The approach is particularly useful where Java implementations are already available and you need to expose implementations as services.

The consumer of a web service uses SEI to invoke the service functions. The SEI directly corresponds to a wsdl:porttype element. In the above example, annotation defined above the interface declaration signifies an interface as a web service interface or Service Endpoint Interface. In the Contract-first approach, you start with the existing WSDL contract, and generate Java class to implement the service. The advantage is that you are sure about what to expose as a service since you define the appropriate WSDL Contract-first.

Again the contract definitions can be made consistent with respect to data types so that it can be easily converted in Java objects without any portability issue. In Chapter 3 we will look at how to develop web services using both these approaches. WSDL contains different elements that can be directly mapped to a Java class that implements the service.

For example, the wsdl:porttype element is directly mapped to SEI, type elements are mapped to Java class types through the use of Java Architecture of XML Binding JAXB , and the wsdl:service element is mapped to a Java class that is used by a consumer to access the web service.

It has various options to generate SEI and the implementation web service class.

As a developer, you need to provide the method implementation for the generated class. The chapter will also cover a brief discussion on the wsdl2java tool. The simple frontend provides simple components or Java classes that use reflection to build and publish web services. It is simple because we do not use any annotation to create web services.

The simple frontend uses factory components to create a service and the client. It does so by using Java reflection API. Interceptors are components that intercept the messages exchanged or passed between web service clients and server components. In CXF, this is implemented through the concept of Interceptor chains. The concept of Interceptor chaining is the core functionality of CXF runtime. Each interceptor in a chain is configurable, and the user has the ability to control its execution.

It defines two abstract methods handlemessage and handlefault. Each of the methods takes the object of type Message as a parameter. A developer implements the handlemessage to process or act upon the message. The handlefault method is implemented to handle the error condition.

Interceptors are usually processed in chains with every interceptor in the chain performing some processing on the message in sequence, and the chain moves forward.

Whenever an error condition arises, a handlefault method is invoked on each interceptor, and the chain unwinds or moves backwards. Interceptors are often organized or grouped into phases. Interceptors providing common functionality can be grouped into one phase.

Each phase performs specific message processing. Each phase is then added to the interceptor chain. The chain, therefore, is a list of ordered interceptor phases. The chain can be created for both inbound and outbound messages.

A typical web service endpoint will have three interceptor chains: Inbound messages chain Outbound messages chain Error messages chain There are built-in interceptors such as logging, security, and so on, and the developers can also choose to create custom interceptors.

The chapter will mainly focus on Interceptors. Service model The Service model, in a true sense, models your service. It is a framework of components that represents a service in a WSDL-like model.

It provides functionality to create various WSDL elements such as operations, bindings, endpoints, schema, and so on. As you can see from the above figure, the service model's primary component is ServiceInfo which aggregates other related components that make up the complete service model. For example, by using a simple frontend, we can create, publish, and consume web services through factory components such as ServerFactoryBean and ClientProxyFactoryBean. These factory classes internally use the service model of CXF.

Data binding Data binding is the key for any web service development. Data binding means mapping between Java objects and XML elements.

As we know, with web service, messages are exchanged as XML artifacts.

So there has to be some way to convert these XML into Java objects and vice versa for the application to process as service and client. Data binding components perform this mapping for you. As a developer, you have the choice of specifying the binding discipline through a configuration file or API.

If no binding is specified, then JAXB is taken as a default binding discipline. For example, annotation specifies that the OrderProcess class will be mapped to complex XSD element type 'processorder' that contains an element 'arg0' of type 'Order' bean. Aegis allows developers to gain control of data binding through its flexible mapping system.

You do not have to rely on annotations to devise the mapping. Aegis reads this XML to perform the necessary binding.CXF provides a flexible deployment model for implementing web services. The method takes an Order bean as a parameter and returns an order ID String value.

These systems may then interact with the web service in a manner prescribed by its definition, using XML-based messages conveyed by Internet protocols.

The following code illustrates the beans. Sections include: XML is a markup language that specifies or describes the format of the data to be exchanged between two parties. Generating crypto key 3. By the end of this chapter the reader will be able to apply different frontends to develop a web service.