Apache CXF开发Web Service 开发Web Service之Kick Start

来源:互联网 发布:怎么优化 编辑:程序博客网 时间:2024/05/17 02:41

Web Service看起来很神秘,不过你使用了ApacheCXF之后,“啊~~~Web Service是这么简单”。本节将介绍Apache CXF是基于JAX-WS创建Web Service。有兴趣的话请看《JAX-WSSpecificantion

 

JAX-WS提供了两种方式来创建Web ServiceCode-FirstContract-First。本节使用Code-First的方式:先创建Java类然后基于Java类生成Web Service组件。任务的第一步就是来创建服务器端组件。想更深一步了解请看:《代码优先(Code-First)和契约优先(Contract-First)的比较》。

 

创建服务器端组件,我们需要执行如下步骤:

创建Service EndpointInterface (SEI),也就是JAVA中的Interface,定义Web Service使用的业务方法。

创建SEI的实现类,声明这是一个WebService

 

 

 

创建ServiceEndpoint Interface (SEI)

 

package demo.order;

 

import javax.jws.WebService;

 

@WebService

public interface OrderProcess {

@WebMethod

   String processOrder(Order order);

}

 

正如你看到的,我们创建了名叫processOrder Service Endpoint Interface (SEI)。这个SEI就像其他JavaInterface一样。定义了接口方法:processOrder。这个方法需要输入Java Bean (Order) 作为参数,并返回字符型的order IDprocessOrder的目的是来处理顾客下订单并返回唯一的订单ID

 

突出显示的地方@WebService Annotation它强调这不是普通的Interface,而是WebService Interface,也就是SEI,用于向客户端外暴露接口调用方法。

 

JAX-WS规范中注解库包含了@WebService AnnotationJAX-WS规范通过注解库的形式将POJO对外暴露为WebService,并定义了WSDLJAVA类之间Web Serive的映射关系。当然javax.jws.WebService还有很多细节内容,这里不再深入讨论。

 

javax.jws.@WebMethod注解是可选的内容,用来制定WebService的操作。@WebMethod Annotation说明了方法名,还有action元素,作为自定义操作的name属性来和WSDL文档的SOAP action元素对应。

 

 

 

创建Java Bean Order

 

package demo.order;

importjavax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Order")

public class Order {

 

         privateString customerID;

         privateString itemID;

         privateInteger qty;

         privateDouble price;

        

         //Contructor

         publicOrder() {

         }

 

         publicString getCustomerID() {

                   returncustomerID;

         }

 

         publicvoid setCustomerID(String customerID) {

                   this.customerID= customerID;

         }

 

         publicString getItemID() {

                   returnitemID;

         }

 

         publicvoid setItemID(String itemID) {

                   this.itemID= itemID;

         }

 

         publicInteger getQty() {

                   returnqty;

         }

 

         publicvoid setQty(Integer qty) {

                   this.qty= qty;

         }

 

         publicDouble getPrice() {

                   returnprice;

         }

 

         publicvoid setPrice(Double price) {

                   this.price= price;

         }

 

}

 

可以清楚的看到,对Order类添加@XmlRootElement注解。Java Architecture for XML Binding (JAXB) 规范中注解库包含了@XmlRootElementJAXB提供了快速而简便的方法将XML模式绑定到Java表示。JAXB隐含了SOAP消息和Java代码的转换细节,节XMLSOAP的解析,开发者不用知道这一过程。CXF默认使用JAXB作为数据绑定工具。

 

@XmlRootElement指定Order类作为XML的根节点。Order对象的属性会默认的注解为@XmlElement@XmlElement用来定义XML的子节点。@XmlRootElement@XmlElement让你自定义XML的命名空间和XML元素。如果没有自定义,JAXB会默认使用相同的名称的属性作为XML子节点。CXF处理JAVA对象到XML的映射关系。

 

 

 

创建SerivceImplementation Class

 

package demo.order;

 

import javax.jws.WebService;

 

@WebService

public class OrderProcessImpl implementsOrderProcess {

 

         publicString processOrder(Order order) {

                   StringorderID = validate(order);

                   System.out.println("Processedorder..." + orderID);

       return orderID;

    }

 

         /**

          * Validates the order and returns the order ID

         **/

         privateString validate(Order order) {

                   StringcustID = order.getCustomerID();

                   StringitemID = order.getItemID();

                   intqty = order.getQty();

                   doubleprice = order.getPrice();

 

                   if(custID != null && itemID != null &&!custID.equals("") &&

                                     !itemID.equals("")&& qty > 0 && price > 0.0) {

                            return"ORD1234";

                   }

                   returnnull;

         }

 

}

 

实现类很简单。也提供了@WebService注解。OrderProcessImpl实现OrderProcess(SEI) 中的processOrder方法。processOrder方法通过validate方法来验证订单是否有效。

 

 

 

基于Spring服务端

 

CXF官方文档(http://cxf.apache.org/docs/why-cxf.html)中写道:

 

Spring is a first class citizen with Apache CXF. CXF supports theSpring 2.0 XML syntax, making it trivial to declare endpoints which are backedby Spring and inject clients into your application.

 

CXF使用基于Spring配置文档来发布Webservice endpoints。正是这一点促使CXF成为Web Service框架里的首选。这里将利用基于Spring配置位置来创建服务端。配置文档为bean.xml

 

<?xml version="1.0"encoding="UTF-8"?>

<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">

 

         <importresource="classpath:META-INF/cxf/cxf.xml" />

         <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

         <importresource="classpath:META-INF/cxf/cxf-servlet.xml" />

 

         <jaxws:endpoint

           id="orderProcess"

          implementor="demo.order.OrderProcessImpl"

           address="/OrderProcess" />

          

</beans>

 

首先定义了文档中使用的命名空间<jaxws>,然后使用<import>导入cxf.xml, cxf-extension-soap.xmlcxf-servlet.xml。这些都是基于Spring的配置文档,定义了CXF的核心组件,提供CXF的运行环境,加载必要基础对象例如:WSDL manager, conduit manager, destination factory manager等。

 

<jaxws:endpoint>节点指定OrderProcess作为JAX-WSendpoint。定义了如下三种属性:

Id - 指定Bean的唯一标示符。

Implementor - 指定Web Service的实现类。

Address – 指定URL访问Web Service的地址。这个是相对地址,具体链接和发布的Web容器相关。

 

<jaxws:endpoint>定义了CXF内置使用JAX-WSfrontend来发布Web Service。节点的内容简短、方便,开发者不需要额外的编写其他内容来发布Web Service

 

 

 

创建基于Spring的客户端

 

<?xmlversion="1.0" encoding="UTF-8"?>

<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">

 

    <jaxws:client id="orderClient"serviceClass="demo.order.OrderProcess"address="http://localhost:8080/orderapp/OrderProcess" >

    </jaxws:client>

 

</beans>

 

client-beans.xml<jaxws:client>指定了客户端使用JAX-WSfrontend。定义了如下三种属性:

Id - 指定Bean的唯一标示符。

serviceClass - 指定Web ServiceSEI

Address – 指定URL访问Web Service的地址。这个绝对地址地址,指向Web Service的访问路径。

 

 

 

创建客户端代码

 

packagedemo.order.client;

 

 

importdemo.order.OrderProcess;

importdemo.order.Order;

 

importorg.springframework.context.support.ClassPathXmlApplicationContext;

 

 

publicfinal class Client {

 

    public Client() {

    }

 

    public static void main(String args[])throws Exception {

         ClassPathXmlApplicationContextcontext

            =new ClassPathXmlApplicationContext(new String[]{"client-beans.xml"});

 

        OrderProcess client = (OrderProcess)context.getBean("orderClient");

                   Order order = new Order();

                   order.setCustomerID("C001");

                   order.setItemID("I001");

                   order.setQty(100);

                   order.setPrice(200.00);

 

        String orderID = client.processOrder(order);

        String message = (orderID == null) ?"Order not approved" : "Order approved; order ID is " +orderID;

                   System.out.println(message);

           

    }

}

 

在代码中使用ClassPathXmlApplicationContext加载client-beans.xml配置文件。

 

 

 

配置Web容器

 

<!DOCTYPEweb-app PUBLIC

 "-//Sun Microsystems, Inc.//DTD WebApplication 2.3//EN"

 "http://java.sun.com/dtd/web-app_2_3.dtd">

 

<web-app>

    <display-name>Archetype Created WebApplication</display-name>

  

    <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>CXFServlet</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>

 

通过web.xmlSpringCXF整合起来。org.apache.cxf.transport.servlet.CXFServlet初始化了CXF运行环境。org.springframework.web.context.ContextLoaderListener加载了beans.xml的配置。

 

 

 

运行Web Service

 

这里使用Maven来作为构建工具。如果想立刻看到效果请使用NetBean,内置了Maven。如果使用eclipse则需要添加m2eclipse插件,参见Eclipse Maven 插件 m2eclipse

 

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <groupId>org.dcb.cxfbook.ch02</groupId>

    <artifactId>orderapp</artifactId>

    <packaging>war</packaging>

    <version>1.0-SNAPSHOT</version>

    <name>orderapp MavenWebapp</name>

   <url>http://maven.apache.org</url>

    <properties>

                   <!-- Version of CXF.Change this to latets version for building against latest CXF distribution-->

       <cxf.version>2.2.3</cxf.version>

    </properties>

    <dependencies>

        <dependency>

           <groupId>junit</groupId>

            <artifactId>junit</artifactId>

           <version>3.8.1</version>

            <scope>test</scope>

        </dependency>

        <dependency>

                       <!-- Apache JAX-WS CXF Dependency forWAR and JAX-WS Client-->

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-frontend-jaxws</artifactId>

           <version>${cxf.version}</version>

        </dependency>

        <dependency>

                       <!-- Apache JAX-WS CXF Dependency forJAX-WS Client-->

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-transports-http</artifactId>

           <version>${cxf.version}</version>

        </dependency>

    </dependencies>

    <build>

       <finalName>orderapp</finalName>

        <plugins>

            <plugin>

                                     <!--Plugin for compiling Java code -->

               <artifactId>maven-compiler-plugin</artifactId>

                <configuration>

                                        <!-- Java version for compiling thesource code-->

                   <source>1.5</source>

                    <target>1.5</target>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.mortbay.jetty</groupId>

               <artifactId>maven-jetty-plugin</artifactId>

               <version>6.1.19</version>

            </plugin>

        </plugins>

    </build>

    <profiles>

        <profile>

            <id>client</id>

            <build>

               <defaultGoal>test</defaultGoal>

                <plugins>

                    <plugin>

                        <groupId>org.codehaus.mojo</groupId>

                        <artifactId>exec-maven-plugin</artifactId>

                        <executions>

                            <execution>

                               <phase>test</phase>

                                <goals>

                                   <goal>java</goal>

                                </goals>

                               <configuration>

                                   <mainClass>demo.order.client.Client</mainClass>

                               </configuration>

                            </execution>

                        </executions>

                    </plugin>

                </plugins>

            </build>

        </profile>

    </profiles>

</project>

 

pom.xml中,添加cxf-rt-frontend-jaxwscxf-rt-transports-http依赖,maven会自动加载相关的jar包,包括CXF, Spring, common, JABX。在Build. Plugins. Plugin节点添加maven-jetty-plugin作为Web容器,方便测试。在profiles.Profile. Build. Plugins. Plugin节点添加exec-maven-plugin提供命令行运行demo.order.client.Client

 

cd orderapp

#启动jetty

mvnjett:run

#执行client

mvn test -Pclient

 

执行client可以直接在IDE (Netbeanor Eclipse)中运行。

 

 

 

 

名词解释:

 

  ApacheCXF = Celtix + XFireApacheCXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXFCXF 继承了 Celtix XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding DataBindingTransport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAPXML/HTTPRESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTPJMS 或者 JBICXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

JCPJavaCommunity Process) 是一个开放的国际组织,主要由Java开发者以及被授权者组成,职能是发展和更新。

CXF frontendsare programming APIs that can be used to develop and publish web services. CXFsupports two types of frontends, JAX-WS and simple frontend.

JAX-WS规范是一组XML web servicesJAVAAPIJAX-WS允许开发者可以选择RPC-oriented或者message-oriented来实现自己的web services

java.lang.annotation,接口Annotation。对于Annotation,是Java5的新特性,JDK5引入了Metedata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形势应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。

Java annotation An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code.[1] Classes, methods, variables, parameters and packages may beannotated. Unlike Javadoc tags, Java annotations can be reflective in that they can be embedded in class files generated by the compiler and may be retained by the Java VMto be made retrievable at run-time.[2] It is possible to create meta-annotations out of the existingones in Java, which makes this concept more sophisticated than in otherlanguages like C#

POJOPlain OldJava Objects)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称。

Web Services Description Language的缩写,是一个用来描述Web服务和说明如何与Web服务通信的XML语言。为用户提供详细的接口说明书。

JAXBJavaArchitecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。

SOAP简单对象访问协议,简单对象访问协议(SOAP)是一种轻量的、简单的、基于 XML 的协议,它被设计成在 WEB 上交换结构化的和固化的信息。 SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议 HTTP),简单邮件传输协议SMTP),多用途网际邮件扩充协议MIME)。它还支持从消息系统到远程过程调用RPC)等大量的应用程序

 

 

 

 

参考内容:

 

http://baike.baidu.com/view/2742297.htmCXF百科

http://baike.baidu.com/view/148425.htmJCP百科

http://baike.baidu.com/view/1865210.htmJAX-WS百科

http://baike.baidu.com/view/612195.htmAnnotation百科

http://en.wikipedia.org/wiki/Java_annotationJAVA Annotation wiki百科

http://baike.baidu.com/view/183175.htmPOJO百科

http://baike.baidu.com/view/160660.htmWSDL百科

http://baike.baidu.com/view/725509.htmJAXB百科

http://baike.baidu.com/view/60663.htmSOAP百科

http://www.jcp.org/en/home/index

http://jcp.org/aboutJava/communityprocess/final/jsr224/index.html

PacktPub.Apache.CXF.Web.Service.Development.Dec.2009第三章

http://book.51cto.com/art/200911/163796.htm

http://cxf.apache.org/docs/why-cxf.html

http://www.oschina.net/p/m2eclipse

 

 

  • orderapp.7z (5.2 KB)
  • 下载次数: 23
原创粉丝点击