Web Services – Java client
January 15, 2012 2 Comments
Robert Mac asked some time ago for Java client for my old post here: Web Services in Ruby, Python and Java. So here it is (sorry for the delay). Simplest possible solution, no jars or IDE needed. Just plain Java 6 JDK.
First we have to generate proxy classes for our Web Service (you need to pass WSDL location, as URL or path to file):
wsimport http://localhost:8080/WSServer/Music?wsdl
wsimport is in /bin folder in your JDK.
Now let’s use them:
public class WSClient {
public static void main(String[] args) {
Music music = new Music();
String[] artists = music.listArtists();
for (String artist : artists) {
System.out.println(artist);
Song[] songs = music.listSongs(artist);
for (Song song : songs) {
System.out.format("\t%s : %s : %d%s\n", new Object[]{song.getFileName(), song.getArtist(), song.getSize(), "MB"});
}
}
}
}
Now compile it and execute with classes generated by wsimport in classpath.
This is all. Simple, isn’t it?
Very interesting. I will surely try to implement something of a nature similar to this in my next project. Great post mate!
Pingback: JavaPins