Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Web Service Client

Status
Not open for further replies.

MalCarne

Technical User
Apr 1, 2005
70
US
I'm new to Java and know just enough about web services to be dangerous in C# and VB. I have to create an app that uses web services and am hitting a wall.
I have a simple class that returns a string that I've deployed as a web service using websphere application developer. I can create a jsp file and use the proxy class that websphere generates to access the webservice and return the string like so:
Code:
   <%
    wsProxy proxy = new wsProxy();
    string result = proxy.getString();
   %>
   <%="result = " + result%>

Simple enough. But when I try to do the same thing from inside of a stand alone class it errors out.
There has to be some simple element that I'm missing here. Unfortunately any docs that I find are either weighed down with low level explanations, or just simply say "use the generated proxy stub" - cool thanks.

Does anyone have any insight?

TIA
 
Look for WSDL2Java that will create the stub and you can try to learn what it does by looking at the code but it gets low level fast.

So do WSDL2Java and then use the generated stuff, that is the best way to use webservices.



Christiaan Baes
Belgium

"My old site" - Me
 
That's where I get a little lost. Can I call the methods in the stub like in the jsp I posted, or am I supposed to add methods to the stub.

Sorry for being a bit thick headed.
 
You cannot use JSP syntax in a plain Java class. The bits inside the JSP <% %> tags IS plain Java but won't tend to compile if simply placed into a plain Java class since there are other things that need to be there that are created for you in a JSP when the page is compiled.

I would think that you would be able to call the stub just as easily from a stand alone class, though.

For example ...
Code:
import [i]the.package.for.the.stub.wsProxy[/i];

public class Example {

  public static void main(String[] args){
    wsProxy proxy = new wsProxy();
    String result = proxy.getString();

    System.out.println(result);
  }
}

You are obviously going to need to learn full Java syntax and stuff if you are needing to write non-jsp Java apps. (It's advisable to know this stuff anyway even if you are doing JSP's).


Tim
 
I'm just trying to wrap my head around things at a very basic level right now (besides, the way my company works translates to "just do it, get it right later"). So, I've tried what timw posted, which is the same thing that I've been trying to get to work for a week (slightly different naming this time, but same principle)

Code:
package wsClient;

import ws.*; //package containing GetStringProxy

public class ClientTest
{

	public static void main(String[] args)
	{
		GetStringProxy proxy = new GetStringProxy();
		String getString = proxy.getString();
		System.out.println(getString);
	}
}

What happens is proxy.getString() results in "unhandled exception type RemoteException" before I even run the class.
Some basic element is missing here, can anyone elaborate?
 
Hmmm, well I can't be expected to know what exceptions to catch from your proxy class!

If a method is declared, for example, as
Code:
public void doSomething() throws Exception1, Exception2 {
   ...some code...
}

then you need to catch those exceptions from the calling code (or have the calling code throw them upwards too, but they will need to be caught at some point). Otherwise it will not compile, let alone run.

Code:
  try {
    someObject.doSomething();
  } catch (Exception1 ex){
    //handle exception1
  } catch (Exception2 ex){
    //handle exception2
  }

If you will be writing Java regularly, you will need to learn the syntax and rules of the language ASAP.


Tim
 
Dian,
Thanks for the tip, I'm already using an eclipse based IDE. I'm finding that I'm not only struggling with the java element of things, but having to learn the IDE at the same time results in further headaches.

I've caught the RemoteException that I mentioned above and now get no def class errors. My reading on this says that it's a missing classpath entry for the jre. That entry is there, so I'm stumped. What's really confusing me is that the jsp runs fine, but compiled code running the exact same bit of code errors. As my research is not showing much, I'm sending up a flag to see if anyone has any insight

I do realize that I need to learn the basics of Java, which I'm trying to do at this time by trying something as basic as this. Unfortunately, I'm not making any headway.
 
What command line are you using to run your stand-alone program? You do need to establish a classpath so that the Java system can locate the classes it needs.

As for learning Java, I personally wouldn't class remoting/JSP/Web Services as easy places to begin. There are enough conceptual layers going on there without having the extra pain of being uncertain about the Java syntax (and IDE perculiarities).

Anyway, post your command line and we'll see what we can do.

Tim
 
Both from the IDE and the command line I get loads of lovely errors.

From the command line - the class "Client" lives in package "client" so from the client directory I run "java Client"
where I get "Exception in thread main NoClassDefFoundError: (wrong name: client/Client)"

My classpath variable is:
C:\Program Files\Java\jre1.6.0_02\bin;.;C:\Program Files\Java\jre1.5.0_11\lib\ext\QTJava.zip;C:\Program Files\javamail-1.4\lib;C:\Program Files\Java\jre1.6.0_01\lib\ext\mysql-connector-java-3.1.14-bin.jar;C:\Program Files\Java\xml-rpc\xmlrpc-2.0-jar;C:\Program Files\Java\xml-rpc\xmlrpc-2.0-applet.jar;

Running from the WebSphere IDE, I receive the following:

Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.ibm.ws.webservices.engine.encoding.DefaultTypeMappingImpl.class$(DefaultTypeMappingImpl.java:128)
at com.ibm.ws.webservices.engine.encoding.DefaultTypeMappingImpl.<init>(DefaultTypeMappingImpl.java:311)
at com.ibm.ws.webservices.engine.encoding.DefaultTypeMappingImpl.getSingleton(DefaultTypeMappingImpl.java:138)
at com.ibm.ws.webservices.engine.encoding.TypeMappingRegistryImpl.<init>(TypeMappingRegistryImpl.java:206)
at com.ibm.ws.webservices.engine.configuration.SimpleEngineConfigurationProvider.getTypeMappingRegistry(SimpleEngineConfigurationProvider.java:201)
at com.ibm.ws.webservices.engine.configuration.SimpleEngineConfigurationProvider.getTypeMappingRegistry(SimpleEngineConfigurationProvider.java:197)
at com.ibm.ws.webservices.engine.WebServicesEngine.getTypeMappingRegistry(WebServicesEngine.java:426)
at com.ibm.ws.webservices.engine.client.Stub.getTypeMapping(Stub.java:617)
at ws.GetStringSoapBindingStub.initTypeMapping(GetStringSoapBindingStub.java:27)
at ws.GetStringSoapBindingStub.<init>(GetStringSoapBindingStub.java:20)
at ws.GetStringServiceLocator.getGetString(GetStringServiceLocator.java:44)
at ws.GetStringServiceLocator.getGetString(GetStringServiceLocator.java:39)
at ws.GetStringProxy._initGetStringProxy(GetStringProxy.java:24)
at ws.GetStringProxy.<init>(GetStringProxy.java:9)
at client.Client.main(Client.java:22)
Caused by: java.lang.IllegalArgumentException: Unterminated quote
at java.text.SimpleDateFormat.compile(Unknown Source)
at java.text.SimpleDateFormat.initialize(Unknown Source)
at java.text.SimpleDateFormat.<init>(Unknown Source)
at java.text.SimpleDateFormat.<init>(Unknown Source)
at com.ibm.ws.webservices.engine.types.Time.<clinit>(Time.java:89)
... 17 more


I'm stuck, any insight is greatly appreciated.

thanks.
 
I resolved the problem. Easy fix. Uninstall any IDE's and anything vaguely java related and reinstall the latest JDK and JRE. Reinstall IDE and all is well.

Small matter of 4 hours to accomplish all of that, but I have a working web service. Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top