在 Eclipse 中创建和部署简单的 Web 服务和 Web 服务客户端
已发表: 2019-10-20按照以下简单步骤在 Eclipse IDE 中创建和部署简单的Web Service
和Web Service Client
。
第1步
安装 Apache Tomcat 并将其添加到服务器选项卡中的 Eclipse——我使用的是Tomcat version 9.0.10
。
第2步
创建一个动态 Web 项目(名称: CrunchifyWS
)

第三步
在/src
文件夹下创建 java 文件。 右键单击 /src 文件夹 -> 新建 -> 类。
- 包:
crunchify.com.web.service
- 名称:
CrunchifyHelloWorld.java

第四步
打开CrunchifyHelloWorld.java
文件并创建简单的 main 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package crunchify . com . web . service ; /** * @author Crunchify.com */ public class CrunchifyHelloWorld { public float addValue ( float value ) { return ( value + 10 ) ; } public float subtractValue ( float value ) { return ( value - 10 ) ; } } |
第 5 步
- 右键单击文件
CrunchifyHelloWorld.java -> Web Services -> Create Web Service
- 选择下图中提到的选项。
- 点击完成

第 6 步
完成所有流程可能需要一些时间,您应该会看到创建了新项目“ CrunchifyWSClient
”。 这是最终的项目结构:

第 7 步
CrunchifyWS
和CrunchifyWSClient
两个项目都应该自动部署到服务器。
此外,Eclipse 会自动打开带有 URL 的Web Service Test Client Window
:http://localhost:8080/CrunchifyWSClient/sampleCrunchifyHelloWorldProxy/TestClient.jsp?endpoint=http://localhost:5922/CrunchifyWS/services/CrunchifyHelloWorld
步骤 8
现在单击addValue(float)
、 subtractValue(float)
并提供输入以检查更新的结果。

你都准备好了。 如果您发现这些步骤有任何困难,请告诉我。
单击“调用”按钮后是否出现以下错误?
1 2 |
Result Exception : java . net . ConnectException : Connection refused ( Connection refused ) Message : ; nested exception is : java . net . ConnectException : Connection refused ( Connection refused ) |

请按照以下步骤操作:
- 右键单击Tomcat
- 单击添加和删除以查看添加的应用程序
- 单击清洁...
- 点击发布
- 单击重新启动

重新启动应用程序或以后如何重新运行WebService?
这是一个方便的 URL:http://localhost:8080/CrunchifyWSClient/sampleCrunchifyHelloWorldProxy/TestClient.jsp

如果你想下载 CrunchifyHelloWorld.wsdl 那么这里是:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
<? xml version = "1.0" encoding = "UTF-8" ?> < wsdl : definitions targetNamespace = "http://service.web.com.crunchify" xmlns : apachesoap = "http://xml.apache.org/xml-soap" xmlns : impl = "http://service.web.com.crunchify" xmlns : intf = "http://service.web.com.crunchify" xmlns : wsdl = "http://schemas.xmlsoap.org/wsdl/" xmlns : wsdlsoap = "http://schemas.xmlsoap.org/wsdl/soap/" xmlns : xsd = "http://www.w3.org/2001/XMLSchema" > < ! -- WSDL created by Apache Axis version : 1.4 Built on Apr 22 , 2006 ( 06 : 55 : 48 PDT ) -- > < wsdl : types > < schema elementFormDefault = "qualified" targetNamespace = "http://service.web.com.crunchify" xmlns = "http://www.w3.org/2001/XMLSchema" > < element name = "subtractValue" > < complexType > < sequence > < element name = "value" type = "xsd:float" / > < / sequence > < / complexType > < / element > < element name = "subtractValueResponse" > < complexType > < sequence > < element name = "subtractValueReturn" type = "xsd:float" / > < / sequence > < / complexType > < / element > < element name = "addValue" > < complexType > < sequence > < element name = "value" type = "xsd:float" / > < / sequence > < / complexType > < / element > < element name = "addValueResponse" > < complexType > < sequence > < element name = "addValueReturn" type = "xsd:float" / > < / sequence > < / complexType > < / element > < / schema > < / wsdl : types > < wsdl : message name = "subtractValueResponse" > < wsdl : part element = "impl:subtractValueResponse" name = "parameters" > < / wsdl : part > < / wsdl : message > < wsdl : message name = "addValueResponse" > < wsdl : part element = "impl:addValueResponse" name = "parameters" > < / wsdl : part > < / wsdl : message > < wsdl : message name = "addValueRequest" > < wsdl : part element = "impl:addValue" name = "parameters" > < / wsdl : part > < / wsdl : message > < wsdl : message name = "subtractValueRequest" > < wsdl : part element = "impl:subtractValue" name = "parameters" > < / wsdl : part > < / wsdl : message > < wsdl : portType name = "CrunchifyHelloWorld" > < wsdl : operation name = "subtractValue" > < wsdl : input message = "impl:subtractValueRequest" name = "subtractValueRequest" > < / wsdl : input > < wsdl : output message = "impl:subtractValueResponse" name = "subtractValueResponse" > < / wsdl : output > < / wsdl : operation > < wsdl : operation name = "addValue" > < wsdl : input message = "impl:addValueRequest" name = "addValueRequest" > < / wsdl : input > < wsdl : output message = "impl:addValueResponse" name = "addValueResponse" > < / wsdl : output > < / wsdl : operation > < / wsdl : portType > < wsdl : binding name = "CrunchifyHelloWorldSoapBinding" type = "impl:CrunchifyHelloWorld" > < wsdlsoap : binding style = "document" transport = "http://schemas.xmlsoap.org/soap/http" / > < wsdl : operation name = "subtractValue" > < wsdlsoap : operation soapAction = "" / > < wsdl : input name = "subtractValueRequest" > < wsdlsoap : body use = "literal" / > < / wsdl : input > < wsdl : output name = "subtractValueResponse" > < wsdlsoap : body use = "literal" / > < / wsdl : output > < / wsdl : operation > < wsdl : operation name = "addValue" > < wsdlsoap : operation soapAction = "" / > < wsdl : input name = "addValueRequest" > < wsdlsoap : body use = "literal" / > < / wsdl : input > < wsdl : output name = "addValueResponse" > < wsdlsoap : body use = "literal" / > < / wsdl : output > < / wsdl : operation > < / wsdl : binding > < wsdl : service name = "CrunchifyHelloWorldService" > < wsdl : port binding = "impl:CrunchifyHelloWorldSoapBinding" name = "CrunchifyHelloWorld" > < wsdlsoap : address location = "http://localhost:8080/CrunchifyWS/services/CrunchifyHelloWorld" / > < / wsdl : port > < / wsdl : service > < / wsdl : definitions > |
如果您在运行此程序时遇到任何问题,请告诉我。
