开发者

Java: Simple SOAP Client

开发者 https://www.devze.com 2023-01-10 23:59 出处:网络
I\'m looking for a SOAP client for Java. 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:

I'm looking for a SOAP client for Java.

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:

<?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);
?>

A开发者_高级运维nd my $response object holds all the information I require.

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

Many thanks in advance,

~Edit 1~

@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

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.


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.

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

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.


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.

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


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.

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.


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...

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

Regards


Take a look at CXF

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号