[Change] using Spring and Apache CXF POJO Web services design and implementation, Part 1: Using CXF and Spring Create Web Services

September 18, 2008

Using the open source Apache CXF Web Services Framework can easily create traditional Java ™ objects (plain old Java object, POJO)-style Web services. This article is Part 1 of this series, will show you how to use Spring and CXF POJO as a Web service to the public. This article also demonstrates CXF integration with the Spring framework.
<! - START RESERVED FOR FUTURE USE INCLUDE FILES - ><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters -> <! - END RESERVED FOR FUTURE USE INCLUDE FILES ->
Introduction

In this article, you will use CXF and Spring to build and develop an order-processing Web services. The Web service processing or verification of customer orders placed, and returns the unique order ID. After reading this article, you will be able to apply the concepts and functions of CXF to build and develop Web services.

System Requirements

To run the examples in this article, make sure you have installed on your computer and set the following software:

  • Java 5 or later
  • Tomcat 5 or later
  • Ant build tool
  • CXF 2.1 binary distribution

After installing the distribution, set the following environment variables:

  • JAVA_HOME (for Java)
  • CATALINA_HOME (for Tomcat)
  • ANT_HOME (for Ant)
  • CXF_HOME (for CXF)

For example, you can set CXF_HOME = C: \ apache-cxf-2.1 and add the following to the PATH environment variable:

  • JAVA_HOME \ bin
  • CATALINA_HOME \ bin
  • ANT_HOME \ bin





Back to top


Why CXF?

Apache CXF is an open source framework that provides for the convenience of Web services to build and develop a reliable infrastructure. It allows to create high-performance and scalable service, you can deploy this service in Tomcat and Spring-based lightweight container, and deployed in the more advanced server, such as Jboss, IBM ® WebSphere ® or BEA WebLogic.

Function

The framework provides the following features:

  • Web services standards support: CXF supports the following Web services standards:
    • Java API for XML Web Services (JAX-WS)
    • SOAP
    • Web Services Description Language (Web Services Description Language, WSDL)
    • Message Transmission Optimization Mechanism (Message Transmission Optimization Mechanism, MTOM)
    • WS-Basic Profile
    • WS-Addressing
    • WS-Policy
    • WS-ReliableMessaging
    • WS-Security
  • Front-end model: CXF provides a front-end modeling concept that allows you to use a different front-end API to create Web services. API allows you to use a simple Bean factory to achieve through the JAX-WAS to create a Web service. It also allows you to create dynamic Web service client.
  • Tool Support: CXF provides for the Java Bean, Web services and WSDL to convert between the different tools. It provides support for Maven and Ant integration, and seamless support for Spring integration.
  • RESTful services support: CXF support for Representational State Transfer (Representational State Transfer, RESTful) service concept and platform support for Java JAX-RS implementation. (Part 2 of this series will provide more information about RESTful services.)
  • Transport and binding of different support: CXF supports a variety of transmission, from XML to comma-separated values (CSV). In addition to supporting SOAP and HTTP protocol binding, it also supports Java Architecture for XML Binding (JAXB) and the AEGIS data binding.
  • Support for non-XML binding: CXF supports non-XML bindings, such as JavaScript Object Notation (JSON) and Common Object Request Broker Architecture (CORBA). It also supports the Java Business Integration (Java Business Integration, JBI) and service component architecture architecture (Service Component Architecture, SCA).





Back to top


Development of Web Services

Let us examine in depth how to use JAX-WS Web services front end to create order processing, and then registered as a Spring Bean. You will use the code first approach, which means that you will first develop a Java class and its annotations for the Web service. To do this, you usually have to perform the following steps:

  1. Create a service endpoint interface (service endpoint interface, SEI), and define a Web service will be open to approach.
  2. Create the implementation class and its annotations for the Web service.
  3. Create beans.xml, and use the JAX-WS service class is defined as the front-end the Spring Bean.
  4. Create web.xml in order to integrate Spring and CXF.

Let us create the order-processing Web service SEI.

SEI created order processing Web services

Create named OrderProcess the SEI, it will have a method processOrder, this method to accept an order Bean and returns a string. processOrder method aims to deal with customers under the orders, and returns a unique order ID.

Listing 1. OrderProcess SEI

                
package demo.order;

import javax.jws.WebService;

@WebService
public interface OrderProcess {
  String processOrder(Order order);
}


As you can see from Listing 1, OrderProcess SEI labeled as just a standard Java interface to Web services. @WebService mark only make the interface a Web service interface. Client or user to use this interface to call the service method. OrderProcess SEI has a service method processOrder, this method takes Order as a parameter, and returns as a string order ID.

Listing 2. OrderProcess service implementation

                
package demo.order;

import javax.jws.WebService;

@WebService(endpointInterface = "demo.order.OrderProcess")
public class OrderProcessImpl implements OrderProcess {

 public String processOrder(Order order) {
  return order.validate();
 }
}


Implementation of the preparation of SEI

To write the first part of the SEI implementation, you also want your implementation class OrderProcessImpl marked as Web services, and provide property endpointInterface, its value is pre-step to create the SEI's fully qualified name. This tells the class to achieve OrderProcess SEI. Because it is the realization of SEI, you must provide order ID, return realization processOrder method.

You have created a SEI and its realization. Use CXF, now you can use JAX-WS service front-end components make it a reality.

Listing 3. Beans.xml configuration file

                
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:jaxws="http://cxf.apache.org/jaxws"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

 <import resource="classpath:META-INF/cxf/cxf.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 

 <jaxws:endpoint 
  
  implementor="demo.order.OrderProcessImpl" 
  address="/OrderProcess" />
          
</beans>


Create a profile CXF

CXF configuration file actually contains the definition of the Spring Bean configuration file. You will use JAX-WS front-end configuration for the OrderProcess Web service creation Bean definitions. beans.xml file <jaxws:endpoint> tag OrderProcess Web services designated as JAX-WS endpoint. This actually means that for internal use CXF JAX-WS to publish this Web service. You must provide the implementation class name, which is OrderProcessImpl, and <jaxws:endpoint> tag address. Web address you provide context-sensitive.

Listing 4. Web.xml Web configuration file

                
<web-app>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>WEB-INF/beans.xml</param-value>
 </context-param>

 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

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

 <servlet-mapping>
  <servlet-name>CXFServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
</web-app>


Finally, you need to do the following:

  • Create web.xml file, load the CXF configuration file of this document.
  • Use the Spring context loader to load the configuration file.
  • Register CXFServlet from the client program to handle all requests.

You have already completed the required server-side component development. Now you can develop services to OrderProcess requesting client component.




Back to top


Development of client

As you can see from Listing 5 to create client-side Bean is very easy, just as easy to create service endpoints. JaxWsProxyFactory used to create OrderProcess Web service client Bean. Bean plant is expected to receive service class (OrderProcess) and your service URL. Bean reference and then by using the factory to create client-side Bean stub OrderProcess.

Listing 5. Client-bean.xml Web client configuration file

                
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:jaxws="http://cxf.apache.org/jaxws"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws 
http://cxf.apache.org/schema/jaxws.xsd">

 <bean 
  factory-bean="clientFactory" factory-method="create"/>
        
 <bean>
  <property name="serviceClass" value="demo.order.OrderProcess"/>
  <property name="address" value="http://localhost:8080/orderapp/OrderProcess"/>
 </bean>
          
</beans>


You will create a Java main program, which uses the Spring context to get the defined client Bean, and then call the processOrder method.

Listing 6. Client-side code

                
public final class Client {

 public Client() {
 }

 public static void main(String args[]) throws Exception {
  ClassPathXmlApplicationContext context 
   = new ClassPathXmlApplicationContext(new String[] 
     {"demo/order/client/client-beans.xml"});

  OrderProcess client = (OrderProcess)context.getBean("client");
    Order order = new Order();

  String orderID = client.processOrder(order);
  System.out.println("Order ID: " + orderID);
  System.exit(0);
 }
}





Back to top


Run the program

Before running the program, on your C: \ packing folder created under the directory structure shown in Figure 1, and the components on which this paper:

  • Java code into the package folder.
  • beans.xml and web.xml into web \ web-inf folder.
  • client-beans.xml to put demo \ order \ client folder.


Figure 1. Code Directory Structure
[Change] using Spring and Apache CXF POJO Web services design and implementation, Part 1: Using CXF and Spring Create Web Services

For building, deploying, and running OrderProcess Web service and client, you will use the Ant tool. Code will be deployed in the Tomcat server. In the c: \ orderapp folder to deploy the code to use ant deploy command.

Applications folder (c: \ orderapp) with Ant build files. After running the above command, your code will be orderapp.war orderapp file deployed in the Tomcat server environment. Now through the CATALINA_HOME \ bin folder to provide catalina start command to start the Tomcat Web server.

orderapp folder created in the webapps folder under Tomcat. Start the server, by entering ant client command to run the application. The output will show order ID (see Figure 2).

Figure 2. Process output


Conclusion

This article briefly describes the functions of CXF framework and demonstrates how it enables you to work without much coding you can create Web services. You understand the context file using Bean Integration Spring and CXF. You also study how the framework will create the Web services infrastructure component of the actual semantics of abstract, and provide you with a focused only on Web services API to create a more simple case.

Now that you have learned to create Web services using CXF basis, please continue to focus on Part 2 of this series, which will show you how to use CXF and Spring will be open for the Restful POJO service.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of [Change] using Spring and Apache CXF POJO Web services design and implementation, Part 1: Using CXF and Spring Create Web Services

  • Getting Started Programming spring Ten Questions

    1, how to study Spring? You can study through the following means spring: (1) spring package download doc directory under MVC-step-by-step and the sample directory are relatively good examples of spring development examples. (2) AppFuse integrates a numbe

  • spring of the jar package Detailed

    Excerpt from: http://blog.csdn.net/exceljava/archive/2007/10/05/1811925.aspx Download the spring package of documents and a variety of packages large projects are often at only some of which we must, if not sure what package when necessary, then take a lo

  • hibernate Technical Study Notes (first)

    Introduction: Model does not match (impedance mismatch) java object-oriented language, object model, its key concepts are: inheritance, association, polymorphism, etc.; database is the relational model, its key concepts are: tables, primary keys, for ...

  • eclipse Hibernate proposals

    Error: The 'Hibernate proposals' proposal computer from the' org.hibernate.eclipse.jdt.ui 'plug-in dod not complete normally. Unable to instantiate the extension. To Avoid this message, disable the 'org.hibernate.eclipse.jdt.ui ...

  • Unable to load bean: type: struts-derfault.xml abnormal

    Struts2 in the use of a lot of friends from time to time we encountered this anomaly, leading to its main reason is because the wrong package, we have to be xwork-2.1.1.jar and struts2-core-2.1.2.jar, replaced xwork-2.0. 4.jar and struts2-core-2.0.14.jar

  • Spring jar package Detailed

    AspectJ directory are in the Spring framework to use AspectJ source code and test program files. AspectJ is the first java application framework provided by the AOP. dist directory is a Spring release package, regarding release package described below in

  • To a generic hibernate example DAO

    Reprint: http://blog.csdn.net/dingx package sgf4web.dao; import java.io.Serializable; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.hibernate.*; import org.hibernate.criterion.*; import org.springframework.

  • Choose Hibernate or iBATIS has its reasons

    Hibernate features: Hibernate powerful database has nothing to do with good, O / R mapping ability, and if you are very proficient in Hibernate, but also for Hibernate to conduct an appropriate package, then your project will be the entire persistence lay

  • What is the appfuse

    First, Appfuse brief introduction Matt Raible are Appfuse developed a guiding entry-level J2EE framework, how to integrate its popular Spring, Hibernate, ibatis, struts, Xdcolet, junit, etc. give the basic framework of the model, the latest version 1.7 is

  • Hibernate.cfg.xml configuration file (including the primary key generation strategy Introduction)

    Hibernate.cfg.xml configuration file: <? xml version = "1.0" encoding = "utf-8"?> <! DOCTYPE hibernate-configuration PUBLIC "- / / Hibernate / Hibernate Configuration DTD / / EN" "hibernate-configuration-2.0.dtd

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries