Apache CXF 101 Win Eclipse开发环境搭建

前言

博客草稿中“SOA生态系统初探”一文一直没有进展,感觉要将SOA、Web Service(WS)、REST等概念阐述清楚还需要一些酝酿。

顶天须得立地,这里记录一些“下里巴人”的实践,主要考察Apache Axis2和Apache CXF两个Java服务开发框架,首先从CXF着手。

 

目录

1 工具

2 Eclipse中Apache CXF配置

3 运行实例

4 参考资料

 

内容

1 工具

Windows 7

Eclipse Juno(4.2.0, JEE version, build-id: 20120614-1722)

Apache CXF 2.4.2(较新版本2.7.11在配置过程中莫名其妙的出现一些错误,回退到较低版本),将CXF解压目录记为CXF_HOME

Apache Tomcat 7.0

 

2 Eclipse中Apache CXF配置

Window->Preferences->Web Services->CXF 2.x Preferences->CXF Runtime中添加运行时,指定CXF解压目录即可

Apache CXF 101 Win Eclipse开发环境搭建_第1张图片

 

3 运行实例

预期目标:自底向上(bottom-up)创建WS,将WS部署到Servlet容器中,观察服务的WSDL、通过浏览器调用WS接口。

 

创建Dynamic Web Project项目

项目结构(添加index.html)

Apache CXF 101 Win Eclipse开发环境搭建_第2张图片

预期的Project Facets

Before

 Apache CXF 101 Win Eclipse开发环境搭建_第3张图片

After

 Apache CXF 101 Win Eclipse开发环境搭建_第4张图片

 

编写服务实现类

package com.spike.cxf;

import javax.jws.WebService;

/**
 * Description: HelloService Implementation<br/>
 * Date: 2014-5-11 下午7:59:33
 */
@WebService(targetNamespace = "http://cxf.spike.com/", portName = "HelloServiceImplPort", serviceName = "HelloServiceImplService")
public class HelloServiceImpl {
    public String getVersion() {
        return "1.0";
    }

    public String greeting(String user) {
        return "Hello " + user + "!";
    }

}

 

基于Dynamic Web Project添加CXF Facets:右键服务实现类,Generate Web Service

Apache CXF 101 Win Eclipse开发环境搭建_第5张图片

 配置WS类型

 Apache CXF 101 Win Eclipse开发环境搭建_第6张图片

服务端点接口(SEI)选择或创建,这里默认

Apache CXF 101 Win Eclipse开发环境搭建_第7张图片

选择服务方法注解,这里默认

Apache CXF 101 Win Eclipse开发环境搭建_第8张图片

CXF Java2WS配置,这里默认

Apache CXF 101 Win Eclipse开发环境搭建_第9张图片

启动服务(实际是Servlet容器)等待

Apache CXF 101 Win Eclipse开发环境搭建_第10张图片

服务发布方式设置,这里默认

Apache CXF 101 Win Eclipse开发环境搭建_第11张图片

 

点击Finish后项目结构

Package Explorer

Apache CXF 101 Win Eclipse开发环境搭建_第12张图片

Project Explorer

Apache CXF 101 Win Eclipse开发环境搭建_第13张图片

Servlet容器的“配置”,端口选用了8082

Apache CXF 101 Win Eclipse开发环境搭建_第14张图片

Apache CXF 101 Win Eclipse开发环境搭建_第15张图片

 

访问服务http://localhost:8082/CXF/services

Apache CXF 101 Win Eclipse开发环境搭建_第16张图片

点击WSDL链接http://localhost:8082/CXF/HelloServiceImplPort?wsdl

Apache CXF 101 Win Eclipse开发环境搭建_第17张图片

生成的WSDL和XSD文件 

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="HelloServiceImplService" targetNamespace="http://cxf.spike.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://cxf.spike.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <wsdl:types>
    <schema xmlns="http://www.w3.org/2001/XMLSchema">
<import namespace="http://cxf.spike.com/" schemaLocation="helloserviceimpl_schema1.xsd"/>
</schema>
  </wsdl:types>
  <wsdl:message name="greeting">
    <wsdl:part name="parameters" element="tns:greeting">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getVersion">
    <wsdl:part name="parameters" element="tns:getVersion">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getVersionResponse">
    <wsdl:part name="parameters" element="tns:getVersionResponse">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="greetingResponse">
    <wsdl:part name="parameters" element="tns:greetingResponse">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="HelloServiceImpl">
    <wsdl:operation name="greeting">
      <wsdl:input name="greeting" message="tns:greeting">
    </wsdl:input>
      <wsdl:output name="greetingResponse" message="tns:greetingResponse">
    </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getVersion">
      <wsdl:input name="getVersion" message="tns:getVersion">
    </wsdl:input>
      <wsdl:output name="getVersionResponse" message="tns:getVersionResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="HelloServiceImplServiceSoapBinding" type="tns:HelloServiceImpl">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="greeting">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="greeting">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="greetingResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getVersion">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="getVersion">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getVersionResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="HelloServiceImplService">
    <wsdl:port name="HelloServiceImplPort" binding="tns:HelloServiceImplServiceSoapBinding">
      <soap:address location="http://localhost:8082/CXF/services/HelloServiceImplPort"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
View Code

 

 

<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://cxf.spike.com/" elementFormDefault="unqualified" targetNamespace="http://cxf.spike.com/" version="1.0">
<xs:element name="getVersion" type="tns:getVersion"/>
<xs:element name="getVersionResponse" type="tns:getVersionResponse"/>
<xs:element name="greeting" type="tns:greeting"/>
<xs:element name="greetingResponse" type="tns:greetingResponse"/>
<xs:complexType name="greeting">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
<xs:complexType name="greetingResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
<xs:complexType name="getVersion">
    <xs:sequence/>
  </xs:complexType>
<xs:complexType name="getVersionResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
View Code

 

 

调用服务接口方法

(1)http://localhost:8082/CXF/services/HelloServiceImplPort/getVersion

Apache CXF 101 Win Eclipse开发环境搭建_第18张图片

(2)http://localhost:8082/CXF/services/HelloServiceImplPort/greeting?arg0=zhoujiagen

Apache CXF 101 Win Eclipse开发环境搭建_第19张图片

 

4 参考资料

 

[1] JAX-WS with Apache CXF and Eclipse [step1] http://angelozerr.wordpress.com/2011/08/23/jaxwscxf_step1/
[2] JAX-WS with Apache CXF and Eclipse [step2] http://angelozerr.wordpress.com/2011/08/24/jaxwscxf_step2/

你可能感兴趣的:(eclipse)