Java:简单的 SOAP 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3463216/
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-08-14 00:59:18  来源:igfitidea点击:

Java: Simple SOAP Client

javasoapwsdl

提问by Eamorr

I'm looking for a SOAP client for Java.

我正在寻找用于 Java 的 SOAP 客户端。

Apache Axis looks very bloated to me. I don't understand why things have to be so complicated in Java. For example, in PHP, all I have to do is:

Apache Axis 在我看来非常臃肿。我不明白为什么 Java 中的事情必须如此复杂。例如,在 PHP 中,我所要做的就是:

<?php
$global_service_wsdl='https://api.betfair.com/global/v3/BFGlobalService.wsdl';
$betfair=new SoapClient($global_service_wsdl);

$params=array("request"=>
    array("header"=>
         array("clientStamp"=>0,"sessionToken"=>$session_token)),"locale"=>""
);
$response=$betfair->getAllEventTypes($params);
?>

And my $response object holds all the information I require.

我的 $response 对象包含我需要的所有信息。

Can anybody suggest how I would implement something like this in Java without too much hassle?

任何人都可以建议我如何在没有太多麻烦的情况下在 Java 中实现这样的东西吗?

Many thanks in advance,

提前谢谢了,

~Edit 1~

~编辑1~

@jarnbjo:

@jarnbjo:

That is very useful to me. The bit I'm stuck on is what imports do I need to get that code to run?

这对我非常有用。我遇到的问题是我需要什么导入才能运行该代码?

I ran this command: sh wsdl2java.sh -o output -a -uri https://api.betfair.com/global/v3/BFGlobalService.wsdl

我运行了这个命令:sh wsdl2java.sh -o output -a -uri https://api.betfair.com/global/v3/BFGlobalService.wsdl

And built the output. Do you think this is quicker than PHP? Also, I've got an "asynchronous" option. Does this mean I can make asynchronous calls? That would be very useful. I'd like to run all this inside a Java-based websocket server.

并构建了输出。你认为这比PHP更快吗?另外,我有一个“异步”选项。这是否意味着我可以进行异步调用?那将非常有用。我想在基于 Java 的 websocket 服务器中运行所有这些。

采纳答案by jarnbjo

Unless you require additional functionality not provided by the SOAP client in the standard Java API, you can use the wsimport tool in the JDK's bin directory (point it to your WSDL URL) and let it generate Java classes for the service facade.

除非您需要标准 Java API 中 SOAP 客户端未提供的附加功能,否则您可以使用 JDK 的 bin 目录中的 wsimport 工具(将其指向您的 WSDL URL)并让它为服务外观生成 Java 类。

With the generated classes, you need some more Java code than in your PHP example to perform the request, but it's still reasonable:

使用生成的类,您需要比 PHP 示例中更多的 Java 代码来执行请求,但这仍然是合理的:

BFGlobalService betfair = new BFGlobalService_Service().getBFGlobalService();

APIRequestHeader header = new APIRequestHeader();
header.setClientStamp(0);
header.setSessionToken("someSessionToken");

GetEventTypesReq req = new GetEventTypesReq();
req.setHeader(header);
req.setLocale("");

GetEventTypesResp response = betfair.getAllEventTypes(req);

This example fails with an error, but probably because the session token is invalid.

此示例失败并出现错误,但可能是因为会话令牌无效。

回答by Maurice Perry

Take a look at CXF

看看CXF

回答by Thorbj?rn Ravn Andersen

Java is statically typed, meaning that the compiler needs to know any method before you can invoke it directly in your source code. This in turn means that you need Java class stubs describing the web service, so you have something to call. There is usually a utility with a web service stack doing exactly this.

Java 是静态类型的,这意味着编译器需要知道任何方法才能直接在源代码中调用它。这反过来意味着您需要描述 Web 服务的 Java 类存根,因此您可以调用一些东西。通常有一个带有 Web 服务堆栈的实用程序就是这样做的。

You might find this question interesting What methods exist to auto-generate java client stubs from WSDL files?

您可能会发现这个问题很有趣存在哪些方法可以从 WSDL 文件自动生成 Java 客户端存根?

回答by Chris Kessel

I'll echo CXF, but with an example of how to use it. However, this assumes you've run the CXF/JAXWS tool to generate the Java code based on the wsdl.

我会回应 CXF,但会举例说明如何使用它。但是,这假设您已经运行了 CXF/JAXWS 工具来生成基于 wsdl 的 Java 代码。

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass( TheGeneratedInterface.class );
factory.setAddress( "hostUrlGoesHere" );
client = (TheGeneratedInterface) factory.create();
return client.methodNameGoesHere( request );

The code to write isn't terribly hard. The harder part actually is figuring out how to generate the necessary Java code from the wsdl. It's not hard, just that you have to get the right command line incantation.

要编写的代码并不难。更难的部分实际上是弄清楚如何从 wsdl 生成必要的 Java 代码。这并不难,只是您必须获得正确的命令行咒语。

回答by leroyse

Check "Send or Post a SOAP message using SAAJ (document/literal)"... It enables you to send and get XML through SOAP and manipulate the content of the SOAP envelope directly without any parsing and interpretation/bindings into java objects such as when you use wsimport...

选中“使用 SAAJ(文档/文字)发送或发布 SOAP 消息”...它使您能够通过 SOAP 发送和获取 XML 并直接操作 SOAP 信封的内容,而无需任何解析和解释/绑定到 Java 对象,例如当您使用 wsimport 时...

http://users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm

http://users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm

Regards

问候