By Siegfried Bolz, 3/27/08 |
|
![]() |
在本文中,我将介绍如何在 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"/>
<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” 目录,则必须创建它)。
有关更多信息,请访问 http://wiki.netbeans.org/FaqEndorsedDirTomcat
下载
NetBeans 6 项目:下载