浏览 271 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-04-02 关键字: soap web services netbeans
如何在NetBeans 6中轻松创建Web Services项目。在这个项目中将用到NetBeans 6.0及其附带的JAX-WS 2.1。
一、Web Services Description Language(WSDL:Web Services 描述语言) 有很多种方式来创建Web Services。其中一种方式是首先创建WSDL文件。首先,你必需明确你的Web Services将要实现一个什么样的功能,从而确定它的输入和输出参数分别是什么。在这个实例中,只实现了一个名为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>
![]() 输入Web Services的名字ServiceImpl,包名为eu.jdevelop.soapwebservices.service,然后将WSDL文件输入框指向你先前创建的wsdl定义文件。 ![]() 点击完成按钮。 下面是为你的Web Services定义正确的URL。打开sun-jaxws.xml,将其中的url-pattern属性设置为“/soapwebservices”。 同样修改web.xml文件中相应位置。 ![]() 编辑index.jsp,将一下代码插入到<body></body>标签中: <jsp:forward page="soapwebservices"></jsp:forward> 到这一步,就可以测试你的Web Services了。右击项目,选择清空并生成。然后运行项目。 在浏览器中输入http://localhost:8080/,回车后可以看到测试效果。点击其中的 Information->WSDL链接,可以查看其WSDL和XML-Schema。 ![]() 下面就可以实现对应的业务逻辑了。 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 三、测试 从http://www.soapui.org下载“soapui”,然后安装并启动它。从实例中导入相应配置文件SOAPWebServices-soapui-project.xml。 打开"Request1",并按下绿色按钮提交请求。 【个人注解】必要的时候,更改一下SOAPWebServices-soapui-project.xml中的端口配置。 恭喜,你的Web Service就运行起来了! 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
| 返回顶楼 | |
|
时间:2008-04-23
不错,不过不要告诉我你的WSDL文件都是自己手敲进去的...
|
|
| 返回顶楼 | |










