Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Simple SOAP Client

Tags:

java

soap

wsdl

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.

like image 675
Eamorr Avatar asked Aug 11 '10 22:08

Eamorr


People also ask

How do I create a WSDL client in java?

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.

How do I call SOAP client?

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


2 Answers

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.

like image 101
jarnbjo Avatar answered Sep 24 '22 00:09

jarnbjo


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?

like image 40
Thorbjørn Ravn Andersen Avatar answered Sep 23 '22 00:09

Thorbjørn Ravn Andersen