簡體   English   中英

如何使用 Java 使用 SOAP web 服務?

[英]How do you consume a SOAP web service with Java?

我需要使用 SOAP web 服務和我正在編寫的 Java 程序。 我的服務器上有一個基本的測試 .NET 服務,位於 a.asmx 文件中。 我在谷歌上找到了一堆復雜的例子,但有人可以為我提供一個簡短的解釋嗎? 這將不勝感激。 謝謝!

這是 my.asmx 文件。

<%@ WebService Language="C#" Class="Example1" %>

using System.Web.Services;

[WebService(Namespace="urn:Example1")]
public class Example1 {

    [ WebMethod ]
    public string sayHello(string name) {
        return "Hello " + name;
    }

}

也許我應該以不同的方式來做這件事? 再次感謝。

您可以使用 JDK 附帶的名為wsimport的工具來解析 WSDL 文件並生成 Java 類。

wsimport http://path/to/your?wsdl -d /desired/output/folder

然后,您可以像這樣使用生成的類:

Example1Endpoint example1 = new Example1Service().getExample1Port();
System.out.println(example1.sayHello("tkcsam"));

您的.Net 服務是您將字符串作為參數發布到的“頁面”嗎? 過去我不得不與其中一些人交談(我不會真正稱它們為“Web 服務”,但無論如何......)。

如果是這種情況,請找出您需要發布的內容。 使用您的 Java 構建您需要饋送到頁面的巨型字符串,然后將其作為參數發送到頁面並等待響應字符串返回。 然后你必須解析那個字符串。

這是非常不雅的,但它曾經是微軟做事的方式。 不確定您的情況是否屬實。 Otherwise if you do have a WSDL to work with then I'd probably use either Jack's answer or follow the comment by djhaskin987 as there are some frameworks listed there that will dynamically generate web service clients based on published web services (that have their WSDL published跟他們)。

JAXWS 是與 java 中的 SOAP Web 服務交互的標准技術。 oracle jdk 中的默認實現是 Metro 堆棧,它具有廣泛的用戶指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM