如何编写 Java 客户端来访问 WSDL 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3492257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:04:18  来源:igfitidea点击:

How to write a Java client to access WSDL file?

javaweb-serviceswsdl

提问by sarah

How can I access the exposed methods in a .wsdlfile using Java? Also, what are the steps involved in writing a Java client and consuming the webservices?

如何.wsdl使用 Java访问文件中公开的方法?另外,编写 Java 客户端和使用 Web 服务所涉及的步骤是什么?

回答by f1sh

In addition to The Elite Gentleman's answer, here are my steps I successfully used to generate classes to be able to use the webservice: Command:

除了The Elite Gentleman 的回答之外,这里是我成功用来生成能够使用网络服务的类的步骤: 命令:

wsimport -Xnocompile -keep -b binding.xml wsdlFile.wsdl

Explanation:

解释:

  • '-Xnocompile' suppresses the generation of .class files
  • '-keep' makes sure the generated Java files wont be deleted (by default, only the .class files remain)
  • '-b ' specifies a binding configuration file. This is necessary! (see below)
  • '-Xnocompile' 禁止生成 .class 文件
  • '-keep' 确保生成的 Java 文件不会被删除(默认情况下,只保留 .class 文件)
  • '-b ' 指定绑定配置文件。这是必须的!(见下文)

I had the problem that the Java classes contained the JAXBElement<Type>wrapper classes. So instead of a class member of type String, I would get the type JAXBElement<String>, which is horrible to use. With the -bswitch for wsimportand the following binding.xmlfile, you get the correct types:

我遇到了 Java 类包含JAXBElement<Type>包装类的问题。所以String我会得到 type JAXBElement<String>,而不是 type 的类成员,这很糟糕。使用以下文件的-b开关,您将获得正确的类型:wsimportbinding.xml

<jaxb:bindings version="2.0"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jaxb:bindings>
        <jaxb:globalBindings generateElementProperty="false" />
    </jaxb:bindings>
</jaxb:bindings>

I hope this helps. wsimportthen generates all the classes you need as well as a class containing methods for all your webservices' methods.

我希望这有帮助。wsimport然后生成您需要的所有类以及包含所有 Web 服务方法的方法的类。

By default, these methods don't have a read timeout (talking network problems while requesting...), see herefor a question I had back then.

默认情况下,这些方法没有读取超时(在请求时谈论网络问题...),请参阅此处了解我当时遇到的问题。

回答by vicsz

I would take a look at getting your IDE to automatically generate everything.

我会看看让您的 IDE 自动生成所有内容。

In Netbeans, the steps are:

在 Netbeans 中,步骤是:

Right Click on your project, click "add Web Service Client", enter the WDSL url and click Finish.

右键单击您的项目,单击“添加 Web 服务客户端”,输入 WDSL url 并单击“完成”。

This will auto-magically create the Java proxy for you.

这将自动神奇地为您创建 Java 代理。

To implement the client in your code, drag and drog the required method (located in Web Services References in your project), into your code.

要在您的代码中实现客户端,请将所需的方法(位于项目中的 Web 服务引用中)拖放到您的代码中。

回答by Buhake Sindi

You will need to need to generate a Java proxy from the WSDL File. You can do this by using Apche CXFor Apache Axis/Axis2to generate Java Proxy/Java Client.

您需要从 WSDL 文件生成 Java 代理。您可以通过使用Apche CXFApache Axis/ Axis2来生成 Java 代理/Java 客户端来完成此操作。

In Java 6, you can also generate java client too. On the JDK/bin there's wsimportto generate Web Service client or in Axis, there's WSDL2Javathat does the same thing like wsimport.

在 Java 6 中,您也可以生成 Java 客户端。在 JDK/bin 上有wsimport来生成 Web 服务客户端,或者在 Axis 中有WSDL2Java,它执行与 wsimport 相同的事情。

回答by Akhilesh Kumar

Use wsimport -keepwhich will give you all the classes. Then write a different service class and copy all the method from the generated service class. Pass your own service class to wsgen -keep -p.It will give you the needful wrapper classes(jax-ws) which you are going to return from your delegate invocation methods.

使用wsimport -keep它会给你所有的类。然后编写一个不同的服务类并从生成的服务类中复制所有方法。将您自己的服务类传递给 . wsgen -keep -p它将为您提供需要的包装类(jax-ws),您将从委托调用方法中返回这些类。

回答by Thorbj?rn Ravn Andersen

I would recommend starting with the Web Service Explorer in Eclipse Java EE. This allows you to investigate any web service given the WSDL.

我建议从 Eclipse Java EE 中的 Web Service Explorer 开始。这允许您调查给定 WSDL 的任何 Web 服务。

The instructions to convert the WSDL to Java depends on which web service library you want to use. If you use Java 6 the Metro stack is built in.

将 WSDL 转换为 Java 的说明取决于您要使用的 Web 服务库。如果您使用 Java 6,则内置了 Metro 堆栈。

回答by D_K

soapUIis one of the softwares allowing you to easily get into WSDL / SOAP world. You can generate SOAP requests with test values to be sent to the server. You can also see the server's SOAP responses. This will let you understand a bit more of WSDL / SOAP. For generating the java code from the wsdl see the recommendations above.

soapUI是让您轻松进入WSDL / SOAP 世界的软件之一。您可以生成带有要发送到服务器的测试值的 SOAP 请求。您还可以查看服务器的 SOAP 响应。这将使您对 WSDL/SOAP 有更多的了解。要从 wsdl 生成 java 代码,请参阅上面的建议。

回答by DENNIS KITHINJI

This is now simple when using your IDE. Lets take netbeans as our sample.

现在使用 IDE 时这很简单。让我们以 netbeans 作为我们的示例。

  1. Open netbeans
  2. create a new project by clicking file and choose new project
  3. choose a java under category and java-application under applications
  4. right click your newly created project and choose new and choose web service client.
  5. Netbeans will ask for a WSDL and client location

    [wsdl and client location][1]

      [1]: https://i.stack.imgur.com/yWQZv.png
    
  6. choose WSDL and enter the URL to the of the WSDL location

  7. click finish and all methods will be created for you

  1. 打开网豆
  2. 通过单击文件创建一个新项目并选择新项目
  3. 选择类别下的 java 和应用程序下的 java-application
  4. 右键单击您新创建的项目并选择新建并选择 Web 服务客户端。
  5. Netbeans 将要求提供 WSDL 和客户端位置

    [wsdl 和客户端位置][1]

      [1]: https://i.stack.imgur.com/yWQZv.png
    
  6. 选择 WSDL 并输入 WSDL 位置的 URL

  7. 单击完成,将为您创建所有方法

Your task from here is to call the methods and pass the parameters

您从这里开始的任务是调用方法并传递参数