Java: Simple SOAP Client - java

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.

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

Related

Python 2.7 : Where is wsdl file stored?

I want to create a Python RESTful Webservice and Java Webservice Client. I am using Python 2.7. I used web.py to create webservice. On client side (Java) , I want a WSDL file to invoke Python Webservice. Where can I find it? I have configured everything on my laptop.
Update: I refer Python simple example and this doesn't creates it.
Thanks !
Looks like you can always generate a url for the .wsdl file like in this example.
I simply created org.apache.http.client.HttpClient in java to invoke this. Though, I am curious to know wsdl file location.
As #Avenet advised, you can use soaplib, which became rpclib, which in turn became spyne. So check that, please.
People usually use the opposite workflow - they create WSDL manually and than generate stubs of code, so you have a good start, which you than extend with your business logic. Advantage is that it can generate both client and server stubs.
But both workflows should work - generating WSDL from a service, or generating code stubs from a WSDL. For learning purpose I would advice you to create WSDL manually.

Easy way to access wsdl from java

I'm looking for an easy way to acces wsdl file from java.
In python with suds I can do somethin like:
self.wsdlClient = Client(WSDL_URL)
wsdlClient.methodName()
Is there a way to connect to wsdl through an URL?
Python is a dynamic language so you can create methods on an object on the fly, like for example a method for each operation of your web service as discovered by reading the WSDL. But Java is not a dynamic language so the Python approach can't be used.
You either have to generate the code at design time (i.e. create a stub) by feeding the WSDL to some tool like wsimport.exe (or other tools) then use the generated code in your application or ...
... you have to parse the WSDL at runtime and create the XML payload based on the information you acquired from the WSDL, in which case you might want to look at the following tool: soap-ws.
The easiest way is to generate a stub.

Accessing a remote service in Java through it's web interface using JAX-WS for SOAP

I have a public EJB class that I want accessible online as a web service. I have generated a WSDL and the SOAP mesasging seems to work. I used soapUI to test the connection. What I'm not clear about is how would I then use this exposed web service. I'd like to try another language like Python to then make calls through that interface. I know that the WSDL is supposed to help a potential client build it's client side code but I'm not sure about how to specify the connection and location and login information if I had that. I know I'm asking a large topic but any information would help. Thanks
Edit: so basicaly I'm just wondering do I have to use tools to generate my client code from the WSDL like axis2. Or whatever Python uses. Or can I write the code by hand? What's generally done. is the server reference included in that WSDL and are call methods generated usually?
Take a look at ZSI
But ZSI is too complex and spends more time to generate proxies
I suggest you to use suds. suds generates the proxies On the fly and so fast, i used it in some projects.
another packages are available:
soaplib
SOAPy
pysimplesoap

Retrieving WSDL for a single operation

Im a Newbie in wsdl parsing and my information about WSDL is very limited.
I have a scenario in which there is a wsdl file with multiple operations and i want to get the wsdl for a specific operation from that. Is there any libs in java or JS to accomplish such a task . Or am i missing something.
Please correct me if there is anything wrong in the question ,
Thanks in Advance
Bijesh
WSDLs represent a single service with specific operations. Those operations belong to the service can't be separated from the service itself. In order to simply invoke one method you'll have to bind against the entire service (and all other operations and defined types). If you are publishing the operation, you'll have to bind and publish all operations and types defined in the service. There isn't really a way around that.
Now, in your case if you are using java and if you are acting as a client, you can do what Alfredo O alluded to and use a SOAP framework's tooling to generate all of the java client code for you. From there it's just a matter of using the actual service class and invoking the method on that class that corresponds to the method you want to call. You'll have to use the entire wsdl, but from the perspective of your code you won't have to worry about calling any other methods than the one that interests you.
Popular choices for generating a java client for a SOAP service are:
Apache CXF
Metro
Apache Axis2
You can use Axis 2 to auto-generate the java code needed to invoke those operations. Use the wsdl2java tool provided by Axis 2.

Axis2 implementing client side

I need to create connection to web service with axis2. I would like to know how can I convert recieved xmls to Java objects. Is there any good tutorials to learn how to do this?
Axis2 is pretty well documented. Make sure to check the axis2 user guide.
On the other hand be aware of that using web services and axis2 is not a piece
of cake sort of think, so read the docs exhausitvly. ;)
Either the XML is part of the SOAP request. In that case, Axis2 will convert it to Java for you.
If XML is part of a "data blob", then you need to do this yourself. There are several options:
You can have a look at the generators which Axis2 uses. Try the docs.
Use an XML OO mapper like Castor.
You can write your own mapper (not really recommended).
If you can influence the sender side, you can try to create XML that follows the rules for XMLEncoder. That would allow you to use the standard Java Serialization API to build objects.
use axis wsdl2java tool and put generated classes to your source files. Then first create a service stub with YourServiceStub(Service_Adress) and use that instance to invoke necessary methods.
for wsdl2java i commonly use:
wsdl2java -uri wsdlLocation -ss -sd -uw -g -o outputLocation

Categories

Resources