Thomson-Blog ( 学习,学习,再学习;努力,努力,再努力。)
在学习的路上不断成长,成功之路就在脚下。

一. 安装Eclipse STP

详细见《Web Service Eclipse STP 安装.doc》文档。安装文档中包括Eclipse STP,Apache CXF相关的Plugins。

 

二. 安装Apache CXF

详细见《Web Service Eclipse STP 安装.doc》文档。安装文档中包括Apache CXF Runtime的安装。

注意:如果安装了Apache Axis2 Tools,注意将相关的Plugins删除,主要包括Axis2_Codegen_Wizard_1.3.0和Axis2_Service_Archiver_1.3.0,否则CXF无法自动从SEI(Service Endpoint Interface) 接口创建WSDL文件。

注意:在启动Eclipse之前,需要在eclipse.ini文件中添加相关选项:-Dorg.osgi.framework.bootdelegation=*原因请具体查看下面的链接:http://wiki.eclipse.org/Eclisep_3.3_runtime_options_for_STP.SC#bootdelegation_configuration。否则可能会出现无法自动创建WSDL文件现象。

 

三. 设置Apache CXF属性

  •          菜单选择Window->Preferences->选择SOA Tools->JAX-WS->设置Apache CXF的runtime位置,例如:C:\apps\apache-cxf-2.1。详细见《Web Service Eclipse STP 安装.doc》文档。
  •          菜单选择Window->Preferences->选择SOA Tools->JAX-WS->Apache CXF->设置WSDL Generation Options中选择SOAP1.1。

注意:可以选择SOAP1.2,不过WSDL自动生成的时候,缺省只生成soap12的binding,不包括SOAP1.1,对于这种情况可能会出现一个问题:如果用VC(例如:VS2005) Web引用的方式调用的话,会出WS-I兼容性问题,因为它不识别soap12的binding。

 

四. 创建JAX-WS项目

  •         菜单选择New->Project...->选择SOA-Tools->JAX-WS Java First Project后选择继续
  •         在Project Name中输入:scxstp01,选择Finish完成

 

五. 创建SEI接口

  •         菜单选择New->Other…->选择Interface后选择继续

 
  •          在Package中输入:org.eclipse.stp.example,在Name中输入:SayHi,选择完成
  •          在SayHi.java文件中添加Interface的方法声明(下面蓝色部分)

package org.eclipse.stp.example;

public interface SayHi {

       public String SayHello(String value);

}

 

六. 增加@WebMethod注释

  •          在Outline视图中选择SayHello方法->菜单中选择SOA->JAX-WS->@Create WebMethod,自动添加相关注释和代码(下面蓝色部分)

package org.eclipse.stp.example;

 

import javax.xml.ws.RequestWrapper;

import javax.xml.ws.ResponseWrapper;

import javax.jws.WebMethod;

 

public interface SayHi {

       @WebMethod(operationName="SayHello", exclude=false)

       @ResponseWrapper(className="org.eclipse.stp.example.SayHelloResponse", localName="SayHelloResponse", targetNamespace="http://example.stp.eclipse.org/")

       @RequestWrapper(className="org.eclipse.stp.example.SayHello", localName="SayHello", targetNamespace="http://example.stp.eclipse.org/")

       public String SayHello(String value);

}

注意:上面定义了OperationName是:SayHello;同时定义了两个类:SayHelloResponse和SayHello

 

七. 增加@WebService注释

  •          在Outline视图中选择SayHi接口->菜单中选择SOA->JAX-WS->@Create WebService,自动添加相关注释和代码(下面蓝色部分)

package org.eclipse.stp.example;

 

import javax.xml.ws.RequestWrapper;

import javax.xml.ws.ResponseWrapper;

import javax.jws.WebMethod;

import javax.jws.WebService;

 

@WebService(name="SayHi", targetNamespace="http://example.stp.eclipse.org/")

public interface SayHi {

       @WebMethod(operationName="SayHello", exclude=false)

       @ResponseWrapper(className="org.eclipse.stp.example.SayHelloResponse", localName="SayHelloResponse", targetNamespace="http://example.stp.eclipse.org/")

       @RequestWrapper(className="org.eclipse.stp.example.SayHello", localName="SayHello", targetNamespace="http://example.stp.eclipse.org/")

       public String SayHello(String value);

}

注意:上面定义的Service的名字是SayHi.

 

八. 创建WSDL文件

  •          在SayHi.java文件中选择@webservice部分->在下面的Annotation Properties中选中javax.jws.soap.SOAPBinding,将缺省的false修改成true;将SOAPBinding中的Style属性从RPC修改成Document。

注意:SOAPBinding中RPC模式不支持RequestWrapper方式

 

l         系统自动添加下面的代码

package org.eclipse.stp.example;

 

import javax.xml.ws.RequestWrapper;

import javax.xml.ws.ResponseWrapper;

import javax.jws.WebMethod;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

 

@SOAPBinding(use=SOAPBinding.Use.LITERAL, style=SOAPBinding.Style.DOCUMENT)

@WebService(name="SayHi", targetNamespace="http://example.stp.eclipse.org/")

public interface SayHi {

       @WebMethod(operationName="SayHello", exclude=false)

       @ResponseWrapper(className="org.eclipse.stp.example.SayHelloResponse", localName="SayHelloResponse", targetNamespace="http://example.stp.eclipse.org/")

       @RequestWrapper(className="org.eclipse.stp.example.SayHello", localName="SayHello", targetNamespace="http://example.stp.eclipse.org/")

       public String SayHello(String value);

}

 

l         保存项目,系统将自动产生SayHi.wsdl文件(注意:正常的情况肯定会生成这个文件),自动产生的文件:SayHello.java和SayHelloReponse.java。

注意:WSDL文件中相关的关于soap:address的部分, <soap:address location="http://localhost:9090/hello"/>,这里的9090端口和hello只是作用在eclipse的客户端服务器方式测试时会用到。如果发布到tomcat服务器,相关的soap:address会自动改变。

l         产生的WSDL的文件如下:

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

<wsdl:definitions name="SayHiService" targetNamespace="http://example.stp.eclipse.org/" xmlns:ns1="http://example.stp.eclipse.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

  <wsdl:types>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.stp.eclipse.org/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://example.stp.eclipse.org/">

<xs:element name="SayHello" type="tns:SayHello"/>

<xs:element name="SayHelloResponse" type="tns:SayHelloResponse"/>

<xs:complexType name="SayHello">

<xs:sequence>

<xs:element minOccurs="0" name="arg0" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="SayHelloResponse">

<xs:sequence>

<xs:element minOccurs="0" name="return" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:schema>

  </wsdl:types>

  <wsdl:message name="SayHelloResponse">

    <wsdl:part name="result" element="ns1:SayHelloResponse">

    </wsdl:part>

  </wsdl:message>

  <wsdl:message name="SayHello">

    <wsdl:part name="parameters" element="ns1:SayHello">

    </wsdl:part>

  </wsdl:message>

  <wsdl:portType name="SayHi">

    <wsdl:operation name="SayHello">

      <wsdl:input name="SayHello" message="ns1:SayHello">

    </wsdl:input>

      <wsdl:output name="SayHelloResponse" message="ns1:SayHelloResponse">

    </wsdl:output>

    </wsdl:operation>

  </wsdl:portType>

  <wsdl:binding name="SayHiServiceSoapBinding" type="ns1:SayHi">

    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

    <wsdl:operation name="SayHello">

      <soap:operation soapAction="" style="document"/>

      <wsdl:input name="SayHello">

        <soap:body use="literal"/>

      </wsdl:input>

      <wsdl:output name="SayHelloResponse">

        <soap:body use="literal"/>

      </wsdl:output>

    </wsdl:operation>

  </wsdl:binding>

  <wsdl:service name="SayHiService">

    <wsdl:port name="SayHiPort" binding="ns1:SayHiServiceSoapBinding">

      <soap:address location="http://localhost:9090/hello"/>

    </wsdl:port>

  </wsdl:service>

</wsdl:definitions>

 

九. 由WSDL创建Java程序

l         选择SayHi.wsdl文件->菜单选择SOA->JAX-WS->Generate Code,显示界面如下:

  l         选择Finish,代码架构如下:

 

l         创建的Java程序:ObjectFactory.java, Package-info.java, SayHi_SayHiPort_Client.java, SayHi_SayHiPort_Server.java, SayHiImpl.java, SayHiService.java文件

 

十. 修改服务器端业务代码

l         修改SayHiImpl.java文件中的实现部分(下面蓝色部分),返回“"Hello: " + arg0

package org.eclipse.stp.example;

import java.util.logging.Logger;

public class SayHiImpl implements SayHi {

    private static final Logger LOG = Logger.getLogger(SayHiImpl.class.getName());

    public java.lang.String sayHello(java.lang.String arg0) {

        LOG.info("Executing operation sayHello");

        System.out.println(arg0);

        try {

            //java.lang.String _return = "";

            java.lang.String _return = "Hello: " + arg0;

            return _return;

        } catch (Exception ex) {

            ex.printStackTrace();

            throw new RuntimeException(ex);

        }

 

l         SayHi_SayHiPort_Server.java相关代码,下面蓝色部分根据SayHi.wsld文件中的:<soap:address location="http://localhost::9090/hello"/>确定,此地址和Eclipse中测试Server启动端口和URI相关。

package org.eclipse.stp.example;

import javax.xml.ws.Endpoint;

public class SayHi_SayHiPort_Server{

    protected SayHi_SayHiPort_Server() throws Exception {

        System.out.println("Starting Server");

        Object implementor = new SayHiImpl();

        String address = "http://localhost:9090/hello";

        Endpoint.publish(address, implementor);

    }

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

        new SayHi_SayHiPort_Server();

        System.out.println("Server ready...");        

       

        Thread.sleep(5 * 60 * 1000);

        System.out.println("Server exitting");

        System.exit(0);

    }

}

 

十一. 修改客户端业务代码

       修改SayHi_SayHiPort_Client.java代码中,关于客户端访问的代码

package org.eclipse.stp.example;

import java.io.File;

import java.net.MalformedURLException;

import java.net.URL;

import javax.xml.namespace.QName;

public final class SayHi_SayHiPort_Client {

    private static final QName SERVICE_NAME = new QName("http://example.stp.eclipse.org/", "SayHiService");

    private SayHi_SayHiPort_Client() {

    }

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

        if (args.length == 0) {

            System.out.println("please specify wsdl");

            System.exit(1);

        }

        URL wsdlURL = null;

        File wsdlFile = new File(args[0]);

        try {

            if (wsdlFile.exists()) {

                wsdlURL = wsdlFile.toURL();

            } else {

                wsdlURL = new URL(args[0]);

            }

        } catch (MalformedURLException e) {

            e.printStackTrace();

        }

        SayHiService ss = new SayHiService(wsdlURL, SERVICE_NAME);

        SayHi port = ss.getSayHiPort(); 

        System.out.println("Invoking sayHello...");

         System.out.println("Invoking sayHello...");

        //*************Changed by Zhenghao***************

        //java.lang.String _sayHi_arg0 = "";

        java.lang.String _sayHello_arg0 = "Zhenghao";

        //*************Ended Change by Zhenghao**********

        java.lang.String _sayHello__return = port.SayHello(_sayHello_arg0);          System.out.println("sayHello.result=" + _sayHello__return);

        System.exit(0);

    }

}

 

十二. 配置tomcat服务器

12.1 安装库文件(支持CXF)

在生产环境中,将CXF下载软件lib和modules目录下的所有jar包拷贝到tomcat安装目录的shared/lib下面。重启Tomcat。

 

12.2 配置tomcat服务器

l         菜单选择File->New->Other…->选择Server->Server后选择Next

l         在Server’s Host name中缺省为localhost,选择Apache->Tomcat v5.5 Server后选择Next

l         Tomcat Server设置中选择Name:缺省,设置Tomcat installation directory中设置tomcat安装的目录,设置缺省的JRE版本,选择Finish完成

l         菜单选择Window->Show View->Other…->选择Server->Server,在Servers的视图中将现在Tomcat v5.5. Server at localhost的状态是Stopped。双击Server想显示Tomcat Server Overview的窗口。

l         在Server Location中选择:Use Tomcat installation(take control of Tomcat installation)

 

l         工具栏中选择保存

12.3 启动tomcat

选中本地的tomcat服务器,右键选择start,服务器的状态从stopped到started

 

十三. 创建Web Service的deployment profile

l         菜单选择File->New->Other…->选择SOA Tools->Deployment Profile后选择Next;

l         在scxstp01中点击wsdl,在File Name中输入:SayHiDeploy,如下图:选择Next;

 

l         在Deployment Description选择继续;Package中选择继续;Target Server选择继续;Summary中选择Finish

l         在wsdl目录下将创建SayHiDeploy.deploy项,右边显示deploy文件,在文件显示框中选择Configuration Tab

 

l         选择Add Target…->在弹出对话框中选择刚才启动的“Tomcat v5.5 Server at localhost”,选择OK,在Server框中将显示刚才添加的target server。

l         选择Create Target

l         选择Deploy Package

l         查看Console Tab,将显示Package Deploy的状态(类似下面的log)

INFO: Creating Service {http://example.stp.eclipse.org/}SayHiService from WSDL: WEB-INF/wsdl/SayHi.wsdl

 

十四. Web浏览器测试

在浏览器中输入:http://localhost:8081/SayHi/services/SayHi?wsdl

 

Deploy后的一个需要注意的地方,binding的地址和名称根据

<wsdl:service name="SayHiService">

- <wsdl:port binding="ns1:SayHiServiceSoapBinding" name="SayHiPort">

  <soap:address location="http://localhost:8081/SayHi/services/SayHi" />

  </wsdl:port>

</wsdl:service>

 

 

在浏览器中输入:http://localhost:8081/SayHi/services/SayHi/SayHello?arg0=bldmickey

- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

- <soap:Body>

- <ns2:SayHelloResponse xmlns:ns2="http://example.stp.eclipse.org/">

  <return>Hello: bldmickey</return>

  </ns2:SayHelloResponse>

  </soap:Body>

  </soap:Envelope>

 

其中SayHello对应的是相应的Operation,其中arg0是参数名称

 

十五. 客户端测试

l         菜单选择Run->Open Run Dialog…->选择Java Application -> SayHi_SayHiPort_Server->选择Run。Console中显示:

Aug 22, 2007 11:15:36 PM sun.reflect.NativeMethodAccessorImpl invoke0

INFO: Started SelectChannelConnector @ 0.0.0.0:9090

Server ready...

l         菜单选择Run->Open Run Dialog…->选择Java Application -> SayHiClient…->选择Run。Console中显示:

INFO: Creating Service {http://example.stp.eclipse.org/}SayHiService from WSDL: file:/D:/2007/CodeWorm/WebService/SourceCode/CXF/scxstp01/wsdl/SayHi.wsdl

Invoking sayHello...

sayHello.result=Hello: Zhenghao

 

l         或者在浏览器中测试

http://localhost:9090/hello?wsdl

结果基本和步骤十四相同,区别在于

- <wsdl:service name="SayHiService">

- <wsdl:port binding="ns1:SayHiServiceSoapBinding" name="SayHiPort">

  <soap:address location="http://localhost:9090/hello" />

  </wsdl:port>

  </wsdl:service>

http://localhost:9090/hello/SayHello?arg0=zhenghao

结果基本和步骤十四相同

 

十六. 部署到生产Tomcat服务器

       将SayHi.war文件直接复制到生产服务器上的:tomcat安装目录下的webapps子目录下。修改文件的属性。例如:

       修改文件的属主。su成tomcat用户,touch SayHi.war。tomcat将自动生成目录SayHi

Web浏览器测试:

http://ServerIP:Port/SayHi/services/SayHi?wsdl

- <wsdl:service name="SayHiService">

- <wsdl:port binding="ns1:SayHiServiceSoapBinding" name="SayHiPort">

  <soap:address location="http://ServerIP:Port/SayHi/services/SayHi" />

  </wsdl:port>

  </wsdl:service>

注意:soap:address将会变成Server的IP地址

 

http://ServerIP:port/SayHi/services/SayHi/SayHello?arg0=bldmickey

- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

- <soap:Body>

- <ns2:SayHelloResponse xmlns:ns2="http://example.stp.eclipse.org/">

  <return>Hello: bldmickey</return>

  </ns2:SayHelloResponse>

  </soap:Body>

  </soap:Envelope>

 

posted on 2008-11-17 11:13  Thomson-Blog  阅读(1696)  评论(0编辑  收藏  举报