在 NetBeans 6 中创建 SOAP Web 服务

 
By Siegfried Bolz, 3/27/08  

netbeanssoap0000

在本文中,我将介绍如何在 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” 创建。
NetBeans new Web Application Project


将它命名为 “SOAPWebServices“,消除 “Context Path” 选项并单击 “Finish“ 按钮。
NetBeans Web Application


现在,打开 “File->New File->Web Services” 并选择 “Web Service from WSDL“。
如果没有此菜单,则需要安装 “Web Service Plugin”(Tools->Plugins->Available Plugins)。
NetBeans Web Service from WSDL


输入 “ServiceImpl“ 作为 Web 服务名称,使用 “eu.jdevelop.soapwebservices.service” 作为包名并浏览到刚才创建的 “webservices.wsdl“ 文件。
NetBeans New Web Service from WSDL


单击 “Finish” 按钮之后,您将看到以下屏幕:
NetBeans Web Service Overview


现在需要修改 URL 模式。打开 “sun-jaxws.xml” 文件并交换 URL 模式与 “/soapwebservices“。
NetBeans sun-jaxws.xml before

NetBeans sun-jaxws.xml after


对 “web.xml“ 文件执行相同操作。
NetBeans web.xml before

NetBeans web.xml after


打开 “index.jsp” 文件并插入 “body“ 标记:

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

NetBeans jsp-forwarding


现在可以测试您所创建的伪 Web 服务。右键单击项目名称并选择 “Clean and Build“。
NetBeans clean and build project


然后,单击 “run“。
NetBeans run project


在浏览器中打开 “http://localhost:8084” 查看结果。
NetBeans running web service


您可以看到 WSDL 和 XML 模式。
NetBeans running WSDL

NetBeans running xml schema


接下来需要插入业务逻辑。添加以下 Java 类接受包结构:
NetBeans project package structure


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



完成之后,您应该能看到类似下图的文件视图:
NetBeans file view


测试

下载、安装和启动 “soapui”,URL 如下:http://www.soapui.org。导入 soapui 项目文件 “SOAPWebServices-soapui-project.xml”(位于本例中)。
打开 “Request1” 并提交请求(单击绿色箭头)。
running soapui with an request-example


恭喜,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” 目录,则必须创建它)。
NetBeans endorsed error with jax-ws

有关更多信息,请访问 http://wiki.netbeans.org/FaqEndorsedDirTomcat


下载

NetBeans 6 项目:下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值