[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
[Change] using Spring and Apache CXF POJO Web services design and implementation, Part 1: Using CXF and Spring Create Web Services

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.

分类:Java 时间:2010-03-31 人气:705
分享到:
blog comments powered by Disqus

相关文章

  • % System environment variables can not even refer to the user environment variables ..% 2010-11-09

    Previously did not know there is such a problem, because in the past not to use% java environment variables in the configuration when System variable path% JAVA_HOME% \ bin User variable JAVA_HOME D: \ Program Files \ Java .... The results can not be

  • Jdk under Linux jdk installation and setting environment variables 2011-04-09

    We install the Linux system software, we often encounter some system environment variables configuration. What is environment variables? How to customize the environment variables? I will do in the following description. First, what is the environmen

  • Set jdk and tomcat with the batch environment variables 2008-10-27

    Studied Java programming should know, is to set the JDK environment variable, this is troublesome for. Here's what I wrote a batch used to set environment variables. We put the following code to copy down and save it as bat file is a batch file, and

  • java object to lua script 2010-05-08

    Need to get rid of the wordFilter , This class is used to convert a Java object lua The string table and . Used in dynamically generated when using lua script You can get to el function inside , Famous original author, but please note , Object I didn

  • To view and modify the Linux environment variables 2010-11-16

    To view and modify the Linux environment variables 2010-03-11 15:54 View the environment variables in Linux 2009-10-30 10:25 System environment variables in the configuration web server and programming have been frequently used, it is necessary to kn

  • Ubuntu-depth understanding of the environment variables 2011-05-17

    Transfer from: http://blog.csdn.net/xiaosu_521/archive/2008/03/09/2160458.aspx Very good at writing, so I have ubuntu's understanding of environment variables, for the author. Environment variable configuration file In Ubuntu, there are several files

  • I used Linux command of the env - display the current user's environment variables 2011-04-09

    I used Linux command of the env - display the current user's environment variables This link: http://codingstandards.iteye.com/blog/994906 (reproduced please indicate the source) Use Description env command to display the current user's environment v

  • I used Linux command of the export - set or display environment variables 2011-08-02

    I used Linux command of the export - set or display environment variables This link: http://codingstandards.iteye.com/blog/1138524 (reproduced please indicate the source) Use Description export command can be used to set or display environment variab

  • Ruby environment variables 2010-02-02

    Ruby installation environment variables Ruby in the installation process will automatically modify the system environment variable PATH, and add the environment variable RUBYOPT (So do not need to configure the Ruby environment variables, this is a v

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.627 (s). 12 q(s)