有两种方式创建WebService
一种是自顶向下。
一种是自底向上。
推荐使用自顶向下的方式,也就是先创建wsdl文件,再生成java代码。
选择WSDL 然后点击下一步,然后取一个名字,然后点击 下一步 出现如下画面:
目标的名称空间是自己可以定义的,相当于你的包名反过来写。
其他的设置可以是默认的,然后点击完成。
然后就生成了一个wsdl文件,
图像化如下:
左边的那个URL 就是你访问service敲的地址,自己定义
一般是本地是:http://localhost:9080/serviceName
右边那个NewOperation 是你要执行的方法,可以改成你要的方法名字
Input是输入的一些参数
Ouput是输出参数
源码如下:
<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions name="NewWSDLFile" targetNamespace="http://www.example.org/NewWSDLFile/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/NewWSDLFile/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:types> <xsd:schema targetNamespace="http://www.example.org/NewWSDLFile/"> <xsd:element name="NewOperation"> <xsd:complexType> <xsd:sequence> <xsd:element name="in" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="NewOperationResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="out" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="NewOperationRequest"> <wsdl:part element="tns:NewOperation" name="parameters"/> </wsdl:message> <wsdl:message name="NewOperationResponse"> <wsdl:part element="tns:NewOperationResponse" name="parameters"/> </wsdl:message> <wsdl:portType name="NewWSDLFile"> <wsdl:operation name="NewOperation"> <wsdl:input message="tns:NewOperationRequest"/> <wsdl:output message="tns:NewOperationResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="NewWSDLFileSOAP" type="tns:NewWSDLFile"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="NewOperation"> <soap:operation soapAction="http://www.example.org/NewWSDLFile/NewOperation"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="NewWSDLFile"> <wsdl:port binding="tns:NewWSDLFileSOAP" name="NewWSDLFileSOAP"> <soap:address location="http://www.example.org/"/> </wsdl:port> </wsdl:service> </wsdl:definitions> |
然后右点击wsdl文件,出现如图所示:
点 Web Service
点 生成Java bean 框架
出现如图:
类型是自顶向下,如果要生成客户端也可以选择
其他默认
点击下一步,出现如图所示:
目标包是你生成的java代码所在的包目录
可以自己定义,我这里采用默认,其他都是默认,点击下一步
出现如图所示:
提示正在生产代码。。。
先不注册到UUID,点击完成
我这里的wsdl文件名是; NewWSDLFile.wsdl
所以找到生成那个java代码,文件名是NewWSDLFileSOAPImpl.java
里面就有一个你需要实现的方法,该方法就是你之前在wsdl文件中定义的需要调用的方法,有一个input的参数是String类型的
package org.example.newwsdlfile;
@javax.jws.WebService (endpointInterface="org.example.newwsdlfile.NewWSDLFile", targetNamespace="http://www.example.org/NewWSDLFile/", serviceName="NewWSDLFile", portName="NewWSDLFileSOAP") public class NewWSDLFileSOAPImpl{
public String newOperation(String in) { // 这里实现 return null; }
} |
然后可以通过 使用Web Service 资源管理器测试 来测试这个service是否正常可用
用法:右点击wsdl文件,选择Web Service 然后选择 使用Web Service 资源管理器测试
然后出来一个界面,在这个界面里找到你的方法名字,点击,然后点击添加输入那个需要的输入参数,这是输入参数是String的 所以 输入一点字符就可以了,然后点击执行。
自底向上的方法如下:
先写要执行的代码,在生成wsdl文件。