The first step is to generate all the classes. Java has a tool called wsimport that generates all the classes given the webservice definition (The url of the definitions normally finishes with ?wsdl). You can find all the documentation here. Here is an example:
wsimport -d ./build -s ./src -p com.your.package <webservice definition url>
It builds the classes in the folder "build" and the source code at the folder "src". With -p you can specify the package where you want the classes to be generated and finally the url.
To start using them, you have to follow 3 simple steps:
WebService ws=new WebService();//The class that extends Service PortType pt=ws.getPortType(); //The port you are going to use, normally httpPort Object response= pt.method();//Invoke the method you want to use with the port
Tips & Tricks
-Every time you create an instance of the webservice, the webservice definition is called. Sometimes java isn't capable of bringing it and throws Exception. A workoround to this problem is to copy the definition and host it on your computer/server and change the reference in the class that extends Service. The only problem with this is that if the webservice changes it's methods, your calls will not work, but it's not going work anyway since the classes do not auto generate every time they are called.
-If the Webservice is hosted on a secure server (https) you will probably have to create and exception so that Java doesn't refuse the connection with the server. I will explain how to do this in a future post
-It's rather complicated to view the soap envelopes(the information you send and recieve from the webservice) in Java with the classes generated by wsimport. A very easy solution is to install Wireshark and filter by XML(Soap messages are xml).
No hay comentarios:
Publicar un comentario