简体   繁体   English

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

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

I am required to consume a SOAP web service with a Java program I am writing.我需要使用 SOAP web 服务和我正在编写的 Java 程序。 I have a basic test .NET service on my server in a.asmx file.我的服务器上有一个基本的测试 .NET 服务,位于 a.asmx 文件中。 There are a bunch of complicated examples that I found on Google but can someone provide a short explination for me?我在谷歌上找到了一堆复杂的例子,但有人可以为我提供一个简短的解释吗? It would be greatly appreciated.这将不胜感激。 Thanks!谢谢!

Here is my.asmx file.这是 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;
    }

}

Maybe there is a different way I should be doing this?也许我应该以不同的方式来做这件事? Thanks again.再次感谢。

You can use a tool that comes with the JDK called wsimport to parse your WSDL file and generate Java classes.您可以使用 JDK 附带的名为wsimport的工具来解析 WSDL 文件并生成 Java 类。

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

You can then use the generated classes like so:然后,您可以像这样使用生成的类:

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

Is your.Net service a "page" that you post a string to as a parm?您的.Net 服务是您将字符串作为参数发布到的“页面”吗? I've had to talk to a few of those in the past (I wouldn't really call them "web services", but anyway....).过去我不得不与其中一些人交谈(我不会真正称它们为“Web 服务”,但无论如何......)。

If that is the case, find out what you need to post.如果是这种情况,请找出您需要发布的内容。 Use your Java to build the giant String that you need to feed to the page and then send it to the page as a parm and wait for the response String coming back.使用您的 Java 构建您需要馈送到页面的巨型字符串,然后将其作为参数发送到页面并等待响应字符串返回。 You'll have to parse that string then.然后你必须解析那个字符串。

This is terribly inelegant, but it used to be how Microsoft did things.这是非常不雅的,但它曾经是微软做事的方式。 Not sure if it is true in your case.不确定您的情况是否属实。 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 with them). 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 is the standard technology for interacting with SOAP webservices in java. JAXWS 是与 java 中的 SOAP Web 服务交互的标准技术。 the default implementation in the oracle jdk is the metro stack, which has an extensive user guide . oracle jdk 中的默认实现是 Metro 堆栈,它具有广泛的用户指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM