程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 在NetBeans 6中創建SOAP Web服務

在NetBeans 6中創建SOAP Web服務

編輯:關於JAVA

在本文中,我將介紹如何在 NetBeans 6 中創建 Web 服務。在此後的文章中 ,我將討論如何在調用 Web 服務操作之前處理 SOAP 消息。

在本例中,我將結合使用 JAX-WS 2.1 與 NetBeans 6.0。

Web 服務描述語言(WSDL)

開發 Web 服務有許多方式。其中之一便是創建 WSDL。首先,您必須了解 Web 服務的應有作用。您需要考慮各 Web 服務操作的輸入和輸出。在本文的例子中, 我們只創建了一個操作,名稱為 “getcalculateValues“。輸入包括兩個數字, 結果為兩數之和。

我們將創建以下兩個文件:

webservices.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="yes"? >
<definitions xmlns:ns1="soapwebservices.jdevelop.eu" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://schemas.xmlsoap.org/soap/encoding/" name="SOAPWebServices" targetNamespace="soapwebservices.jdevelop.eu">
<types>
<xsd:schema>
<xsd:import namespace="soapwebservices.jdevelop.eu" schemaLocation="webservices.xsd"/>
</xsd:schema>
</types>
<message name="calculateValues">
<part name="calculateValues" element="ns1:calculateValues"/>
</message>
<message name="calculateValuesResponse">
<part name="calculateValuesResponse" element="ns1:calculateValuesResponse"/>
</message>
<portType name="SOAPWebServices">
<operation name="getCalculateValues">
<input message="ns1:calculateValues"/>
<output message="ns1:calculateValuesResponse"/>
</operation>
</portType>
<binding name="SOAPWebServicesPortBinding" type="ns1:SOAPWebServices">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getCalculateValues">
<soap:operation soapAction="urn:http://blog.jdevelop.eu/services/getCalculateValues"/&g t;
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="SOAPService">
<port name="WebServices" binding="ns1:SOAPWebServicesPortBinding">
<soap:address location="http://blog.jdevelop.eu:80/services"/>
</port>
</service>
</definitions>

webservices.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"? >
<xs:schema xmlns:ns1="http://blog.jdevelop.eu/soapwebservices.xsd" xmlns:tns="soapwebservices.jdevelop.eu" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="soapwebservices.jdevelop.eu" version="1.0">
<xs:element name="calculateValues">
<xs:complexType>
<xs:sequence>
<xs:element name="value1" type="xs:decimal"/>
<xs:element name="value2" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="calculateValuesResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="result" type="xs:decimal"/>
<xs:element name="errormessage" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

創建 NetBeans 應用程序

啟動 NetBeans 6 並創建一個新的 “Web Application” 創建。

將它命名為 “SOAPWebServices“,消除 “Context Path” 選項並單擊 “Finish“ 按鈕。

現在,打開 “File->New File->Web Services” 並選擇 “Web Service from WSDL“。

如果沒有此菜單,則需要安裝 “Web Service Plugin”(Tools- >Plugins->Available Plugins)。

輸入 “ServiceImpl“ 作為 Web 服務名稱,使用 “eu.jdevelop.soapwebservices.service” 作為包名並浏覽到剛才創建的 “webservices.wsdl“ 文件。

單擊 “Finish” 按鈕之後,您將看到以下屏幕:

現在需要修改 URL 模式。打開 “sun-jaxws.xml” 文件並交換 URL 模式與 “/soapwebservices“。

對 “web.xml“ 文件執行相同操作。

打開 “index.jsp” 文件並插入 “body“ 標記:

<jsp:forward page="soapwebservices"></jsp:forward>

現在可以測試您所創建的偽 Web 服務。右鍵單擊項目名稱並選擇 “Clean and Build“。

然後,單擊 “run“。

在浏覽器中打開 “http://localhost:8084” 查看結果。

您可以看到 WSDL 和 XML 模式。

接下來需要插入業務邏輯。添加以下 Java 類接受包結構:

ServiceImpl.java

package eu.jdevelop.soapwebservices.service;
import eu.jdevelop.soapwebservices.CalculateValues;
import eu.jdevelop.soapwebservices.CalculateValuesResponse;
import eu.jdevelop.soapwebservices.SOAPWebServices;
import eu.jdevelop.soapwebservices.wrapper.impl.CalculateValuesWrapper;
import javax.jws.WebService;
/**
* This is the Service-Implementation of the Web Service. Here are the
* operations which can be called from web clients.
*
* @author Siegfried Bolz
*/
@WebService(serviceName = "SOAPService", portName = "WebServices", endpointInterface = "eu.jdevelop.soapwebservices.SOAPWebServices", targetNamespace = "soapwebservices.jdevelop.eu", wsdlLocation = "WEB- INF/wsdl/ServiceImpl/webservices.wsdl")
public class ServiceImpl implements SOAPWebServices {
public CalculateValuesResponse getCalculateValues(CalculateValues calculateValues) {
try {
CalculateValuesWrapper wrapper = new CalculateValuesWrapper();
return wrapper.getResult(calculateValues);
} catch (Exception x) {
throw new IllegalStateException(x);
}
}
} // .EOF

ILogic.java

package eu.jdevelop.soapwebservices.logic;
/**
* Use this interface to create logic-implementations for
* each web service operation.
*
* @author Siegfried Bolz
*/
public interface ILogic<T, V> {
public T doAction(V var)
throws Exception;
} // .EOF

CalculateValuesLogic.java

package eu.jdevelop.soapwebservices.logic.impl;
import eu.jdevelop.soapwebservices.CalculateValues;
import eu.jdevelop.soapwebservices.CalculateValuesResponse;
import eu.jdevelop.soapwebservices.logic.ILogic;
import java.math.BigDecimal;
/**
* This implementation is normaly used for executing operations.
* Here we calculate some values.
*
* @author Siegfried Bolz
*/
public class CalculateValuesLogic implements ILogic<CalculateValuesResponse, CalculateValues>{
public CalculateValuesResponse doAction(CalculateValues var) throws Exception {
CalculateValuesResponse response = new CalculateValuesResponse();
try {
/**
* Simple addition of two values
*/
BigDecimal value1 = var.getValue1();
BigDecimal value2 = var.getValue2();
double sum = value1.doubleValue() + value2.doubleValue();
response.setResult(BigDecimal.valueOf(sum));
} catch (Exception x) {
/**
* On errors, return a valid bean with values. Do not send null!
*/
CalculateValuesResponse errorResponse = new CalculateValuesResponse ();
errorResponse.setResult(BigDecimal.valueOf(0.0));
errorResponse.setErrormessage("An error has occurred!");
return errorResponse;
}
return response;
}
} // .EOF

IWrapper.java

package eu.jdevelop.soapwebservices.wrapper;
/**
* Use this interface to create wrapper-implementations for
* each web service operation.
*
* @author Siegfried Bolz
*/
public interface IWrapper<T, V> {
public T getResult(V var)
throws Exception;
} // .EOF

CalculateValuesWrapper.java

package eu.jdevelop.soapwebservices.wrapper.impl;
import eu.jdevelop.soapwebservices.CalculateValues;
import eu.jdevelop.soapwebservices.CalculateValuesResponse;
import eu.jdevelop.soapwebservices.logic.impl.CalculateValuesLogic;
import eu.jdevelop.soapwebservices.wrapper.IWrapper;
/**
* The wrapper calls the logic-implementation. Exchange or modify
* the wrapper if you want to use other logic-implementations.
*
* @author Siegfried Bolz
*/
public class CalculateValuesWrapper implements IWrapper<CalculateValuesResponse, CalculateValues>{
public CalculateValuesResponse getResult(CalculateValues var) throws Exception {
CalculateValuesLogic logic = new CalculateValuesLogic();
return logic.doAction(var);
}
} // .EOF

完成之後,您應該能看到類似下圖的文件視圖:

測試

下載、安裝和啟動 “soapui”,URL 如下:http://www.soapui.org。導入 soapui 項目文件 “SOAPWebServices-soapui-project.xml”(位於本例中)。

打開 “Request1” 並提交請求(單擊綠色箭頭)。

恭喜,Web 服務可以正常運行。

潛在問題

如果遇到以下錯誤:

Caused by: java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI (from jar: file:/C:/temp/SOAPWebServices/build/web/WEB-INF/lib/jaxb- impl.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.1 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.5.0/docs/guide/standards/)

這表示您的 JAX-WS 版本比 JAXB 2.0 和 JAX-WS 2.0 更新,它是 JDK 6 的 一部分。.

要解決此問題,只需將 $NETBEANS_HOME/java1/modules/ext/jaxws21/api/ 中的所有 JAR 文件復制到 $TOMCAT_INSTALL_DIR/endorsed/(如果不存在 “endorsed” 目錄,則必須創建它)。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved