Java Webservice Client (Best Way)

What is the best way to write a java client for a web service?

First of all you need wsdl. Some service providers might distribute Java client classes, but WSDL is the safest approach.

Once you have it, run tool like wsdl2java for apache-cxf or analogous in apache-axis against it. It will create a bunch of Java classes (you only need the client side). These tools are also capable of generating server-side code, hence the impression you had that they aim service developers.

This is all you need - the client classes will handle XML marshalling/unmarshalling and HTTP connectivity for you. Just use appropriate stub implementing WS endpoint interface.

You can also use WebServiceTemplate from spring-ws portfolio.

How to create a java client that consumes a webservice?

Igor, just use wsimport with your web service url - you will get generated classes for WebService and then just invoke service in that way:

ServiceGenerateFromWSImportWhichIsTheSameAsYour iService = 
new ServiceGenerateFromWSImportWhichIsTheSameAsYour().
getServiceGenerateFromWSImportWhichIsTheSameAsYourPort();

// now on iServie instance you can invoke method from your webservice
// but you have to use stub classes generated by wsimport

iService.myMethodWhichGetFileList(List<SoapFileStubGeneratedClass> sopaFiles);

And wsimport is standard java tool in jdk instal folder

More on wsimport tool you can find here:

wsimport doc

Using wsimport in your case will be:

wsimport -p generated_classes -s generated_sources http://127.0.0.1:8080/myapp/WSPA?wsdl

and you will find .class files in folder generated_classes and .java files in folder generated-sources

standalone java webservice client

This question all depends on the following:

  • The JDK version of your Java compiler.
  • Your WSDL version (there's 1.0, 1.2 and 2.0).

Basically, if you are using Java annotations to generate web services, then you'll need Java 5 related Web Services libraries (which supports annotations).

Some articles on Using Java Web Services with annotations (JAX-WS):

  • Introducing JAX-WS 2.0 With the Java SE 6 Platform
  • JAX-WS 2.0

I'll start from generating Web Service client with Java that doesn't support annotations. The well known client that generates WSDL to Java is Apache Axis (the last version is 1.4 released in 22 April 2006). This basically takes a WSDL definition and generates it back to client. It supports the old version of WSDL (1.0) and crashes if you use the newer versions of WSDL (1.2 and 2.0).

What this basically does, it takes your WSDL and generates a java Proxy that communicates to your Web Service. It can allow RPC based as well as XML based communication.

For Java that supports annotations there are, effectively, 2 ways of doing this:

  • Using Java's own wsimport command (the executable is found under the JDK_HOME/bin/ folder).
  • Using 3rd Party libaries such as Apache Axis 2 (which effectively replaces Apache Axis and supports WSDL version 2.0) or Apache CXF (which supports WSDL up to 1.2).

To use wsimport, you basically need to go to a shell command (or write a script) and effectively do something of this effect:

wsimport -d [outputdir] wsdl_file

and your java proxy will be found in the [outputdir] folder.

wsimport is found in JDK 1.6 (I don't know if it exists in earlier versions). More source here, and here.

For Apache Axis, Apache Axis 2 or Apache CXF, there's a WSDL2Java class file that does source code generation.

Here's a guide on how to use WSDL2Java in Apache CXF and in Apache Axis 2.

I hope this helps you in some way as much as I spent like 30 minutes off work doing this. :-)

Ways to create clients of soap web services

To call a SOAP web service you have to send it a properly formatted SOAP message that respects the service's contract. That's it!

So basically to create a client you just need to build that XML message, for example, given this service, you can do the following (I'm assuming Java since you tagged the question like that - but it applies to any programming language):

1) use string concatenation (this is as basic as you can get):

int number1 = 1;
int number2 = 2;
String myMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ " <soap:Body>"
+ " <Add xmlns=\"http://tempuri.org/\">"
+ " <intA>" + number1 + "</intA>"
+ " <intB>" + number2 + "</intB>"
+ " </Add>"
+ " </soap:Body>"
+ "</soap:Envelope>";

then do a (basic) POST that to the service taking care to provide it with the required HTTP headers (like SOAPAction, etc).

2) manually build an XML document to send to the service, something like using SAAJ for example.

3) use the service WSDL and feed it to a tool (wsimport, wsdl2java, etc) from some framework/library (JAX-WS, Axis2, CXF, etc) to get back a client that abstracts the call to a simple method invocation taking Java objects and returning Java objects.

4) Any other method you can think of to create the SOAP message and send it as a POST request (I see you tagged the question JAXB, that will do too...).

Calling a SOAP web service is so common these days that nobody bothers to spend time building a client when there are tools for almost every language to generate one from the WSDL. It's boilerplate code.

People just want a client, to shove it in the project, to use it, and to move on to doing more important stuff in their application. That's why most go for point 3).

I see the ways of developing a clients for SOAP web services are independent of the web service implementation

Yes, you can have a service in a programming language/technology stack and the client in another. The SOAP protocol is the common denominator. Respect the protocol and service contract and the service can work with any client.

Easy way to generate web service clients in Java WITHOUT using Axis

SoapUI can generate the soap clients for many multiple java soap stacks like axis 1 & 2, xfire, GStep, JAX-WS, JAX-RPC, JBoss WS, Oracle Proxy, cxf, .Net, GStep. Here is the tutorial on how to do it.

What is the best way to create a client-server application with a Java Web Start program?

For your purpose you better create a webservice which communicates over http for exchanging data. I would recommend doing this in Java or maybe creating a WCF in C#.

Tutorials in Eclipse for java webservice: http://www.vogella.com/articles/REST/article.html#first_project

In netbeans: http://netbeans.org/kb/docs/websvc/jax-ws.html

The first tutorial is a restful service which is pretty popular and easy to grasp.
Good luck!

Webservice client, should i keep service or port instance?

Keeping the Port around is definitely the best performing option, but keep in mind the thread safety aspects:

http://cxf.apache.org/faq.html#FAQ-AreJAXWSclientproxiesthreadsafe%3F



Related Topics



Leave a reply



Submit