[英]How to consume a top-down web service?
我一直在嘗試使用 Eclipse、Tomcat 8 和 Axis 實現的相當復雜的 web 服務遇到各種問題。 這是我第一次使用 web 服務,所以我決定開始嘗試一些更簡單的東西,以確保我知道自己在做什么。 這是我目前用來從 Eclipse 生成服務的 WSDL。
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.example.org/WeatherService/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="WeatherService"
targetNamespace="http://www.example.org/WeatherService/">
<wsdl:types>
<xsd:schema targetNamespace="http://www.example.org/WeatherService/">
<xsd:element name="GetCurrentTemperatureRq">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="city" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetCurrentTemperatureRs">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="temperature" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="GetCurrentTemperatureRq">
<wsdl:part element="tns:GetCurrentTemperatureRq" name="parameters"/>
</wsdl:message>
<wsdl:message name="GetCurrentTemperatureRs">
<wsdl:part element="tns:GetCurrentTemperatureRs" name="parameters"/>
</wsdl:message>
<wsdl:portType name="WeatherServicePortType">
<wsdl:operation name="GetCurrentTemperature">
<wsdl:input message="tns:GetCurrentTemperatureRq"/>
<wsdl:output message="tns:GetCurrentTemperatureRs"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WeatherServiceSOAP" type="tns:WeatherServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetCurrentTemperature">
<soap:operation soapAction="http://www.example.org/WeatherService/GetCurrentTemperature"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WeatherService">
<wsdl:port binding="tns:WeatherServiceSOAP" name="WeatherServiceSOAP">
<soap:address location="http://localhost:80/WeatherServiceApp/WeatherService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
這個 WSDL 不是我自己的,取自 教程。 我沒有嚴格遵循本教程,因為 Axis 使用 wsdl2java 而不是 wsimport 進行代碼生成,我讓 Eclipse/Axis 處理 web.xml。
我可以成功生成 Web 服務並使用 Eclipse 的 Web 服務的資源管理器進行測試工作正常。 我的問題是我想使用生成的客戶端進行測試,而不是僅在本地對 web 服務進行測試。 我生成了 web 服務客戶端,並在客戶端內部編寫了一個非常簡單的程序來嘗試測試它。
package client;
import java.rmi.RemoteException;
import org.example.www.WeatherService.*;
public class WeatherServiceClient {
public static void main(String [] args) throws RemoteException {
WeatherServiceSOAPStub stub = new WeatherServiceSOAPStub();
GetCurrentTemperatureRq rq = new GetCurrentTemperatureRq();
stub.getCurrentTemperature(rq);
}
}
運行這個我得到一個錯誤。
Nov 19, 2019 11:55:05 AM org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
Exception in thread "main" AxisFault
faultCode: {http://xml.apache.org/axis/}Server.NoEndpoint
faultSubcode:
faultString: No endpoint
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}exceptionName:org.apache.axis.NoEndPointException
{http://xml.apache.org/axis/}stackTrace:No endpoint
at org.example.www.WeatherService.WeatherServiceSOAPStub.getCurrentTemperature(WeatherServiceSOAPStub.java:150)
at client.WeatherServiceClient.main(WeatherServiceClient.java:11)
{http://xml.apache.org/axis/}hostname:fake_hostname
No endpoint
at org.example.www.WeatherService.WeatherServiceSOAPStub.getCurrentTemperature(WeatherServiceSOAPStub.java:150)
at client.WeatherServiceClient.main(WeatherServiceClient.java:11)
當我嘗試這個時,主要的 web 服務正在 Tomcat 中運行。
如何解決此端點問題? 我的 WSDL 的地址有問題還是需要更改soapAction?
我的問題似乎與 WSDL 或至少我如何生成客戶端有關。 Instead of generating the Web Service and the Web Service Client simultaneously, I instead just generated the Web Service first and deployed it to Tomcat. 似乎 Axis 生成了一個稍微不同的 WSDL 用於我的目的。
So instead of generating the client based off of the original WSDL, I based it off of the WSDL generated by Axis which was at http://localhost:8080/WeatherService/services/WeatherServiceSOAP?wsdl
in my case.
對於那些感興趣的人,這里是 Axis 生成的 WSDL。
<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://www.example.org/WeatherService/" xmlns:intf="http://www.example.org/WeatherService/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/WeatherService/">
<!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
-->
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/WeatherService/">
<element name="GetCurrentTemperatureRq">
<complexType>
<sequence>
<element name="city" type="xsd:string" />
</sequence>
</complexType>
</element>
<element name="GetCurrentTemperatureRs">
<complexType>
<sequence>
<element name="temperature" type="xsd:string" />
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>
<wsdl:message name="GetCurrentTemperatureResponse">
<wsdl:part element="impl:GetCurrentTemperatureRs" name="GetCurrentTemperatureRs"></wsdl:part>
</wsdl:message>
<wsdl:message name="GetCurrentTemperatureRequest">
<wsdl:part element="impl:GetCurrentTemperatureRq" name="GetCurrentTemperatureRq"></wsdl:part>
</wsdl:message>
<wsdl:portType name="WeatherServicePortType">
<wsdl:operation name="GetCurrentTemperature" parameterOrder="GetCurrentTemperatureRq">
<wsdl:input message="impl:GetCurrentTemperatureRequest" name="GetCurrentTemperatureRequest"></wsdl:input>
<wsdl:output message="impl:GetCurrentTemperatureResponse" name="GetCurrentTemperatureResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WeatherServiceSOAPSoapBinding" type="impl:WeatherServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="GetCurrentTemperature">
<wsdlsoap:operation soapAction="http://www.example.org/WeatherService/GetCurrentTemperature" />
<wsdl:input name="GetCurrentTemperatureRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="GetCurrentTemperatureResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WeatherService">
<wsdl:port binding="impl:WeatherServiceSOAPSoapBinding" name="WeatherServiceSOAP">
<wsdlsoap:address location="http://localhost:8080/WeatherService/services/WeatherServiceSOAP" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.