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);
?>
And 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.
Generate the client code as follows: In the Project Explorer, right-click your client project's WSDL file, and then select WebLogic Web Services > Generate Web Service Client from the drop-down menu, as Figure 1 shows. This will open the New Web Service Client dialog that Figure 2 shows.
Calling this method directly is deprecated. Usually, SOAP functions can be called as methods of the SoapClient object; in situations where this is not possible or additional options are needed, use SoapClient::__soapCall().
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With