首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Apache Geronimo 部署 Web 服務完整教程:以 Amazon AWS 為例

     編輯:Java 相關知識

Geronimo Web 服務支援矩陣

基於 J2EE 1.4,整合 Apache Axis + Apache Scout,支援:

  • JSR 109 (Enterprise Web Services 1.1)
  • JAX-RPC
  • SAAJ 1.2
  • JAXR 1.0

環境準備

  1. JDK 1.4.2+,設 JAVA_HOME。
  2. Apache Axis 1.1+,設 AXIS_HOME。
  3. Amazon AWS WSDL 文件(AWSECommerceService.wsdl)。
  4. AWS 訂閱 ID。

從 WSDL 生成客戶端代碼

設定 Classpath (setenv.bat)

set AXIS_HOME=c:\axis-1-2
set CLASSPATH=.
set CLASSPATH=%AXIS_HOME%\lib\axis.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\commons-discovery.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\commons-logging.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\jaxrpc.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\saaj.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\log4j-1.2.8.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\wsdl4j.jar;%CLASSPATH%

運行 WSDL2Java

java org.apache.axis.wsdl.WSDL2Java AWSECommerceService.wsdl

生成 com 目錄,包含 Stubs、Callers、Data Types。

編寫業務封裝類

package com.amazon.xml.AWSECommerceServer;
public class AmazonClient {
    public Items[] lookupISBN(String isbn) throws Exception {
        AWSECommerceServiceLocator locator = new AWSECommerceServiceLocator();
        AWSECommerceServicePortType port = locator.getAWSECommerceServicePort();
        ItemLookup lookup = new ItemLookup();
        lookup.setAssociateTag("YOUR_TAG");
        lookup.setSubscriptionId("YOUR_SUB_ID");
        ItemLookupRequest req = new ItemLookupRequest();
        req.setMerchantId("All");
        req.setItemId(new String[]{isbn.trim()});
        req.setResponseGroup(new String[]{"Medium","OfferFull","Variations","Images"});
        lookup.setRequest(new ItemLookupRequest[]{req});
        ItemLookupResponse resp = port.itemLookup(lookup);
        return resp.getItems();
    }
}

JSP 前端調用

<%@ page import="com.amazon.xml.AWSECommerceServer.*" %>
<%
    AmazonClient client = new AmazonClient();
    Items[] items = client.lookupISBN(request.getParameter("isbn"));
    if (items != null) {
        for (Items item : items) {
            // 迭代 Item 顯示書名、作者、圖片、價格...
        }
    }
%>

打包部署描述符

web.xml (Servlet 2.3)

<web-app xmlns="https://java.sun.com/xml/ns/j2ee" version="2.3">
    <display-name>Amazon Sample</display-name>
    <welcome-file-list><welcome-file>searchAmazon.jsp</welcome-file></welcome-file-list>
</web-app>

geronimo-jetty.xml

<web-app xmlns="https://geronimo.apache.org/xml/ns/web/jetty"
         configId="amazonclient" parentId="amazonClient">
    <context-root>/amazonclient</context-root>
    <context-priority-classloader>true</context-priority-classloader>
</web-app>

構建與部署 WAR

jar cf amazon.war .
cp amazon.war $GERONIMO_HOME/
java -jar bin/deployer.jar deploy amazon.war

啟動 Geronimo:java -jar bin/server.jar,訪問 https://localhost:8080/amazonclient/searchAmazon.jsp。

效果截圖

Amazon Web 服務查詢結果

擴展建議

  • 將 POJO 替換為無狀態 Session EJB,部署到 Geronimo。
  • 嘗試 Google、webMethods 等其他 Web 服務,流程相同。
  • 使用 Eclipse WTP 0.7+ 簡化開發(當時對 Geronimo 支援有限)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved