Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a web service (described by a wsdl) from java

Tags:

Knowing nothing of web services, I'm just trying to call some "isAlive" service that is described by a wsdl.

This seems to me like something that should take no more than 2-5 lines of code but I can't seem to find anything but huge long examples involving 3rd party packages etc.

Anyone has any ideas? If it is always suppose to be long maybe a good explanation as to why it has to be so complicated will also be appreciated. I'm using Eclipse and the wsdl is SOAP.

like image 889
codekitty Avatar asked Dec 01 '09 14:12

codekitty


People also ask

How do you call a service in Java?

To call a Java service, you can discover a Java class and generate an external service. You can then use the external service in a service flow to call the Java service.

What is WSDL in Web Services in Java?

Web Services Description Language (WSDL) In Java Web Development World, WSDL is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information.


2 Answers

JDK 6 comes with jax-ws, everything you need to develop a client for a web service.

I'm unable to find some simple enough examples to post , but start at https://jax-ws.dev.java.net/

Edit: here's a simple example - a client for this web service: http://xmethods.com/ve2/ViewListing.po?key=427565

C:\temp> md generated C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\wsimport -keep -d generated http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl 

Create PrimeClient.java which look like:

import javax.xml.ws.WebServiceRef; import com.microsoft.webservices.*;  //the above namespace is from the generated code from the wsdl.   public class PrimeClient {  //Cant  get this to work.. @WebServiceRef(wsdlLocation="http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl")   static PrimeNumbers service;    public static void main(String[] args) {     try {     service = new PrimeNumbers();       PrimeClient client = new PrimeClient();       client.doTest(args);     } catch(Exception e) {       e.printStackTrace();     }   }    public void doTest(String[] args) {     try {       System.out.println("Retrieving the port from the following service: " + service);       PrimeNumbersSoap pm = service.getPrimeNumbersSoap();       System.out.println("Invoking the getPrimeNumbersSoap operation ");       System.out.println(pm.getPrimeNumbers(100));     } catch(Exception e) {       e.printStackTrace();     }   } }  

Compile and run:

C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\javac -cp generated PrimeClient.java C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\java -cp .;generated PrimeClient Retrieving the port from the following service: com.microsoft.webservices.PrimeN umbers@19b5393 Invoking the getPrimeNumbersSoap operation 1,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97 
like image 131
nos Avatar answered Nov 09 '22 01:11

nos


There are plugins for IDE's which generate the needed code to consume a web service for you.

After the plugin generates you the base methods you simply call a web service like that:

TransportServiceSoap service = new TransportServiceLocator().getTransportServiceSoap(); service.getCities(); 

Have a look at http://urbas.tk/index.php/2009/02/20/eclipse-plug-in-as-a-web-service-client/

like image 34
JCasso Avatar answered Nov 09 '22 01:11

JCasso