24

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.

3
  • Can you please provide the IDE you use? Commented Dec 1, 2009 at 14:07
  • Can you look at the wsdl, whether it's SOAP or any other protocol? I guess it's SOAP but just to make sure. Commented Dec 1, 2009 at 14:09
  • It is soap : xmlns:ns2="schemas.xmlsoap.org/soap/encoding and I'm using eclipse IDE Commented Dec 1, 2009 at 14:17

4 Answers 4

6

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 
Sign up to request clarification or add additional context in comments.

4 Comments

Are you certain that you don't simply call the class itself? I can see no network callup in your code? Does it work when you are offline too?
@AngeloNeuschitzer The wdsl contains the network address and the generated code takes care of setting up the connection to that service. This obviously does not work if you are offline as the server will be unreachable
Both the links in the answer are dead.
The actual answer, which is supposed to be in the link, is unreachable due to a dead link.
4

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/

2 Comments

Great link. Solved my problem instantly.
the urban.tk is down, broken link!
1

There are three ways to write a web service client

  1. Dynamic proxy
  2. Dynamic invocation interface (DII)
  3. Application client

Example for Dynamic Proxy Client

import java.net.URL; import javax.xml.rpc.Service; import javax.xml.rpc.JAXRPCException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceFactory; import dynamicproxy.HelloIF; public class HelloClient { public static void main(String[] args) { try { String UrlString = "Your WSDL URL"; // String nameSpaceUri = "urn:Foo"; String serviceName = "MyHelloService"; String portName = "HelloIFPort"; System.out.println("UrlString = " + UrlString); URL helloWsdlUrl = new URL(UrlString); ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service helloService = serviceFactory.createService(helloWsdlUrl, new QName(nameSpaceUri, serviceName)); dynamicproxy.HelloIF myProxy = (dynamicproxy.HelloIF) helloService.getPort( new QName(nameSpaceUri, portName), dynamicproxy.HelloIF.class); System.out.println(myProxy.sayHello("Buzz")); } catch (Exception ex) { ex.printStackTrace(); } } } 

I hope , this would solve your question.

2 Comments

eclipse cannot find these imports. what packages do I need to add to my build path?
All packages are come with JDK except "import dynamicproxy.HelloIF;", you could remove this package.
-2

The easiest I've found so far to use is the Idea IntelliJ wizard which - using Metro libraries - generate a very small code snippet which works fine with Java 6.

1 Comment

downvote? Its true :) Works fine with Java 6 without any extra libraries, that is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.