web service

bbvic
bbvic used Ask the Experts™
on
I am working on web service client.
the below code was copied and a little bit changed.
But this code does not work..
Woudl you please help how i can solve this


    import java.net.URL;

    import javax.xml.rpc.ServiceFactory;
    import javax.xml.rpc.Service;
    import javax.xml.rpc.Call;
   
    import javax.xml.namespace.QName;

    public class Main {

      public static void main(String[] args) throws Exception {

        System.setProperty( "javax.xml.rpc.ServiceFactory", "com.sun.xml.rpc.client.ServiceFactoryImpl");
        ServiceFactory factory = ServiceFactory.newInstance();
        URL wsdlLocation = new URL("http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl");
       
        String targetNamespace = "http://www.themindelectric.com/wsdl/net.xmethods.services.stockquote.StockQuote/";
        QName serviceName = new QName(targetNamespace, "net.xmethods.services.stockquote.StockQuoteService");
        QName portName = new QName(targetNamespace, "net.xmethods.services.stockquote.StockQuotePort");
        QName operationName = new QName("urn:xmethods-delayed-quotes", "getQuote");

        // create service
        Service service = factory.createService(wsdlLocation, serviceName);
        // create call
        Call call = service.createCall(portName, operationName);
        // invoke the remote web service
        Float result = (Float) call.invoke(new Object[] {"BEAS"});
        System.out.println(result);
     
      }

    }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
What error are u getting? I think that webservice is down as i am getting a Connection timed out. only the WSDL is present at Xmethods i tryed invoking it But it says "Connection timed Out".

Author

Commented:
Have an error on "create service" line.

did you run the above code?

Author

Commented:
when it compiles the above java code,  the it gets an error on the below "create service" comment.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
no man i used AXIS client :) which is a java based client i use eclipse to generate the client..

Author

Commented:
is there any other function instead invoke?
I need to get a return value from web service server based on WSDL.

Commented:
your service is working. use axis to call this;

NetXmethodsServicesStockquoteStockQuoteService service = new NetXmethodsServicesStockquoteStockQuoteServiceLocator();
try {
      NetXmethodsServicesStockquoteStockQuotePortType call = service.getNetXmethodsServicesStockquoteStockQuotePort();                  
      float f = call.getQuote("BEAS");
      System.out.println(f);                  
} catch (Exception e) {
      e.printStackTrace();
}             


for more info;
http://ws.apache.org/axis/java/user-guide.html

Author

Commented:
NetXmethodsServicesStockquoteStockQuoteService

what is it? where is this class defined..??

Author

Commented:
How did you axis? I did download axis-bin-1_4. where can i put this one?

Author

Commented:
is there any way to modify using the orginal code, not using axis?
 is it not a simple way?
do i have to use wsdl2java generator?

Author

Commented:
In fact, I am using NetBeans IDE 5.5 and there is a function to generate web service client from WSDL.

but it does not create a client side and it has some errors..

Has anyone used NetBeans to generate web service client?

Commented:
>>what is it? where is this class defined..??
generate related class by wsdl2java

>>How did you axis? I did download axis-bin-1_4. where can i put this one?
you need some jars which comes with axis; axis.jar, jaxrpc.jar, commons-logging.jar, commons-discovery.jar, saaj.jar, wsdl4j.jar

for more info;
http://ws.apache.org/axis/java/user-guide.html

>>is there any way to modify using the orginal code, not using axis?
i dont know but it very is by axis. i call your webservice successfully by axis.

i am using eclipse :( but it runs at every IDE
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
I tryed using Netbeans it says"error: rpc/encoded wsdls are not supported in JAXWS 2.0."

Than means you can't use Netbeans for generating code for this WSDL the only way to go is use AXIS 2 wsdl 2 java its and eclipse plugin easy to use.

see this

http://ws.apache.org/axis2/tools/1_2/eclipse/wsdl2java-plugin.html

Commented:
I usually modify my batch file which creates javas from wsdl to where i speficed output directory then i move these files to my project. so i is very easy and IDE independent ;)

the command like this;
java -cp %MYCLASSPATH% org.apache.axis.wsdl.WSDL2Java -o %OUTPUT_DIR% -p %OUTPUT_PAC% -a -v -s xxxxxxxxxxxxxxxxx.wsdl

Author

Commented:
ok.....axis and tomcat installation were succeed.

Then how to use the tool for wsdl2java?

here is wsdl location, for example,...

http://soap.amazon.com/schemas2/AmazonWebServices.wsdl

is this rignt command?

java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -p tempv1.ws http://soap.amazon.com/schemas2/AmazonWebServices.wsdl

after it creates java files, where those will be saved?

Thanks,,


Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
Ya that command seems fine  any way for running WSDL2java sucessfully u need to have eight jars in the class path they are

axis.jar, saaj.jar, jaxrpc..jar, and so on do one thing put all the jar files in that axis lib folder in class path before running the below command.

java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -p tempv1.ws http://soap.amazon.com/schemas2/AmazonWebServices.wsdl

Author

Commented:
well..I have this message when it opens http://127.0.0.1:8080/axis/happyaxis.jsp

Found SAAJ API ( javax.xml.soap.SOAPMessage ) at an unknown location
Found JAXP implementation ( javax.xml.parsers.SAXParserFactory ) at an unknown location
Found Activation API ( javax.activation.DataHandler ) at an unknown location

what does this message mean?
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
this means yur SAAJ.jar is still not in classpath or is not loaded. Do u have weblogic installed in your system? then u need to do lot more.

Author

Commented:
weblogic? what version and what should i download from there?
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
no don't download any thing else :) just set the saaj.jar in classpath
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006
Commented:
Now your next step will be to set all the jars in the classpath and run WSDl2java command.

forget about that happy axis page

now open a command prompt and follow what i tell here.

type "java"  in the command prompt and see if u get the default help menu when u do that if you are getting that then u just need to set classpath and go ahead. But if you are not getting that then u also need to set Path variable.


C:\> java

your must get the help menu else you need to set the path as follows.

c:\> SET PATH JAVA_INSTALLATION_FOLDER/ bin

i.e place where u installed java. like c:\>SET PATH c:\progra~1\java\jdk1.5\bin

after this step you need to set all the jars present in axis\lib folder to classpath.

like this.

C:\> SET CLASSPATH %CLASSPATH%;.;c:\axis\lib\axis.jar;c:\axis\lib\saaj.jar; so on.

then after setting all the jars in class path execute your command.

java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -p tempv1.ws http://soap.amazon.com/schemas2/AmazonWebServices.wsdl

thats it you must have all ur classes ready to use.




Author

Commented:

1. Tomcat.

C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib

2. axis

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\axis\WEB-INF\lib

3. I set all jar in classpath using "environment variables"

4. run cmd

5. c:\>java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -p tempv1.ws http://soap.amazon.com/schemas2/AmazonWebServices.wsdl

6. will it be create .java ??

Commented:
the batch file given below will create java files under D:\ws\samples\sample1 which can be changed by changing OUTPUT_DIR params. your package will be org.ysnky.ws.temp and you can change to anything else.

first of all create "D:\ws\samples" directory and set LIB_HOME variables with your jars' path. and then your files will be created under D:\ws\samples\sample1 as a package. copy this package to your project and use the code i posted above.



set OUTPUT_DIR=D:\ws\samples\sample1
set LIB_HOME=C:\axis-1_2_1

mkdir %OUTPUT_DIR%

set MYCLASSPATH=%LIB_HOME%\lib\axis.jar;
set MYCLASSPATH=%MYCLASSPATH%;%LIB_HOME%\lib\jaxrpc.jar;
set MYCLASSPATH=%MYCLASSPATH%;%LIB_HOME%\lib\commons-logging.jar;
set MYCLASSPATH=%MYCLASSPATH%;%LIB_HOME%\lib\commons-discovery-0.2.jar;
set MYCLASSPATH=%MYCLASSPATH%;%LIB_HOME%\lib\saaj.jar;
set MYCLASSPATH=%MYCLASSPATH%;%LIB_HOME%\lib\wsdl4j-1.5.1.jar;

java -cp %MYCLASSPATH% org.apache.axis.wsdl.WSDL2Java -o %OUTPUT_DIR% -p org.ysnky.ws.temp -a -v -s http://soap.amazon.com/schemas2/AmazonWebServices.wsdl

pause
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
6. will it be create .java ??

yes it will do that :)

Author

Commented:
I have this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/axis/wsdl/WSDL2java

Author

Commented:
In fact, I have this issue..

after testing a SOAP Endpoint, I have xml format like the below.
is this correct one??

<?xml version="1.0" encoding="UTF-8" ?>
<soapenv:Envelope">
  <soapenv:Body>
    <getVersionResponse
     soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <getVersionReturn
       xsi:type="xsd:string">
         Apache Axis version: 1.4 Built on Apr 22, 2006 (06:55:48 PDT)
      </getVersionReturn>
    </getVersionResponse>
  </soapenv:Body>
</soapenv:Envelope>


------------------- based on instruction -----------------------------

<?xml version="1.0" encoding="UTF-8" ?>
<soapenv:Envelope
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <getVersionResponse
     soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <getVersionReturn
       xsi:type="xsd:string">
         Apache Axis version: 1.1 Built on Apr 04, 2003 (01:30:37 PST)
      </getVersionReturn>
    </getVersionResponse>
  </soapenv:Body>
</soapenv:Envelope>
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
i am not getting why are u trying to invoke the default version web service which comes with axis.???
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
>>> Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/axis/wsdl/WSDL2java

are the jar files in class path???  its not able to find that class file that means the axis.jar may not be in the class path.

Author

Commented:
I add full path name: c:\jdk1.5.0_06\bin and added the required .jar files
-----------------------------------------------
C:\Program Files\axis_lib\axis.jar;
C:\Program Files\axis_lib\commons-discovery-0.2.jar;
C:\Program Files\axis_lib\commons-logging-1.0.4.jar;
C:\Program Files\axis_lib\jaxrpc.jar;
C:\Program Files\axis_lib\saaj.jar;
C:\Program Files\axis_lib\log4j-1.2.8.jar;
C:\Program Files\axis_lib\xml-apis.jar;
C:\Program Files\axis_lib\xercesImpl.jar;
C:\Program Files\axis_lib\wsdl4j-1.5.1.jar;
-------------------------------------------------
INTO  the bin directory.


And I typed the below

c:\>java org.apache.axis.wsdl.WSDL2Java -o D:\temp\ws -d Session -s -p tempv1.ws http://soap.amazon.com/schemas2/AmazonWebServices.wsdl

But it still gives me an error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/axis/wsdl/WSDL2java

did I miss something?

Commented:
use my directory sturcture i posted above.
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006
Commented:
I got it you knopw what Windows has a small problem with "program files" when you run in commandline mode.
your class path entries must be
-----------------------------------------------
C:\Progra~1\axis_lib\axis.jar;
C:\Progra~1\axis_lib\commons-discovery-0.2.jar;
C:\Progra~1iles\axis_lib\commons-logging-1.0.4.jar;

and so on
-------------------------------------------------

trust me this is one the most basic mistake and hard to find.

or else do one thing shift the axis folder to c:\ and then continue :) that time you class path entries will be only C:\axis_lib\axis.jar; and so on.

Author

Commented:
It creates the needed java files..
then..how to get a value through the web service..

can you show how to do it? such as including import, public, and so on..

---sample that i have---
NetXmethodsServicesStockquoteStockQuoteService service = new NetXmethodsServicesStockquoteStockQuoteServiceLocator();
try {
      NetXmethodsServicesStockquoteStockQuotePortType call = service.getNetXmethodsServicesStockquoteStockQuotePort();                  
      float f = call.getQuote("BEAS");
      System.out.println(f);                  
} catch (Exception e) {
      e.printStackTrace();
}            



Author

Commented:
java files..

----------

TestWS.java
TestWSBindingImpl.java
TestWSBindingStub.java
TestWSLocator.java
TestWSPortType.java

--------------

Author

Commented:
do i need to run java file first to make class file?

Author

Commented:
will it be generated java class also?

Author

Commented:
I mean class file...

well....I cannot see java.io.Serializable..

Author

Commented:
well...assume that i have all java files , then..how to use it??

Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
Ok now you need to write your own client which uses these classes to call the operation you like to invoke.

So write a java class with main method in it and then create a object of type proxy which got generated ;) and invoke suitable operation.

Author

Commented:
It is working now.
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial