wood burning stoves 2.0*
The moose likes Web Services and the fly likes WSDL2Java Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "WSDL2Java Question" Watch "WSDL2Java Question" New topic
Author

WSDL2Java Question

vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
I just started using Webservices, so please excuse me if I missed something obvious.

I have the wsdl url. I need to pass the url to a java class and to generate the java class with the wsdl, so i can get the interface with all the method names. Is there anyway we can do this, so I can get the list of methods as a map or something.

As of now I use WSDL2Java in command line and the java file is created and stored in y machine. But I need to do this in java.

Many use only ANT scripts to do this, but as per my requirement we cannot use ANT.

My .jws file is



So I executed

and I got HelloWorldService.java in my machine.



Is there any way I can get only the method names from this class. I need to display this information.

Thanks.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
You should probably use
JAX-RPC: Dynamic Invocation Interface (DII) or JAX-WS: javax.xml.ws.Dispatch client
instead.

To read the WSDL simply read it through an HTTP connection and then extract the information that you need directly from it.

The classes generated by WSDL2Java should not be used as a canonical representation of the service interface - they are simply a convenience java adapter for a static (i.e. non-changing) endpoint definition.

As you are dynamically exploring the WSDL you should stick to the definition found within the WSDL which is the actual service contract - not some approximated translation as provided by WSDL2Java.
[ September 25, 2007: Message edited by: Peer Reynders ]
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Thanks for your response.

So you mean I do not need to use WSDL2Java at all. I also have a Client with which I accessed the wsdl.



I was also able to see the output.

To read the WSDL simply read it through an HTTP connection and then extract the information that you need directly from it.


Could you explain this a bit more. Are you saying that I need to parse the xml and look for the tag "wsdl:message" in <wsdl:message name="HelloWorldRequest">
<wsdl art name="data" type="xsd:string" />
</wsdl:message>

Am I correct or did I miss something.

Thanks again.

P.S. I am looking at the 2nd link you gave as the first was kind of similar to what I did.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by vjy chin:
Are you saying that I need to parse the xml and look for the tag "wsdl:message" in <wsdl:message name="HelloWorldRequest">
<wsdl art name="data" type="xsd:string" />
</wsdl:message>


Yes. However you are not looking for "wsdl:message" you are looking for "{http://schemas.xmlsoap.org/wsdl/}message" - that is, you need to be namespace aware. So it is probably easier with XPath. For an example see here.

Ronald Bourret: XML Namespaces FAQ
James Clark: XML Namespaces
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Thanks for the response.

I could not go through the links fully. I would do that ASAP.

I tried running the example you had written and I was not able to run it. I changed a bit.


[ September 25, 2007: Message edited by: vjy chin ]
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by vjy chin:
I tried running the example you had written and I was not able to run it. I changed a bit.


The XPath libraries have are only standard with Java 1.5.
With 1.4 you have to use Xalan's XPathAPI which works a bit differently.

Xalan
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Thanks again. I am using Java 1.5 version. Actually I had the questions in the code part. I apologize for not mentioning it.

Please look at the code part, I have added my questions in the code part


Thanks again.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by vjy chin:
Is this correct? If not how should I do this for my example


Yes, provided you use "wsdl" as the prefix in your XPath query and the WSDL complies with WSDL 1.0.
For WSDL 2.0 you have to use "http://www.w3.org/ns/wsdl".

What value should I set for this and how do I determine the value.


Well, you are going to need multiple expressions - so its time to brush up on XPath

"/wsdl:definitions/wsdl:messages/attribute::name/child::text()"
or
"/wsdl:definitions/wsdl:messages/@name/text()"

Should give you a org.w3c.dom.NodeList of the message names (haven't tested it though).

However these messages are only part of a "web operation" within the WSDL if
  • The message name is referenced in a portType's operation definition.
  • That operation is bound in a binding.
  • The binding is used in the service's port definition.

  • (reality is even a bit more complicated than that).
    What you are really interested in are the binding's operation names.

    You will need to change the content of that xmlSource variable - that needs to be the WSDL itself.
    [ September 25, 2007: Message edited by: Peer Reynders ]
    vjy chin
    Ranch Hand

    Joined: Feb 17, 2005
    Posts: 279
    Thanks for the reply.

    I actually got something working though not much. I have completely removed the name spaces and I am looking only for certain attributes.

    The xml file which I have from the URL is


    As of not I am only going to be looking for wsdl:message attributes.

    The code I have is a little modified version of yours.



    Ok, the output I get is



    This is well and good, but I also need to get the wsdl:part and the type (xsd:string) which tells me type of parameter.

    Do I need to change my expression1 (String expression1 = "/schema/element/attribute::*"; ) statement?

    How can I do that? Any suggestions.

    Thanks again.
    [ September 28, 2007: Message edited by: vjy chin ]
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    Given the expression that you are using I don't even know what you are parsing. That expression is finding

    <schema>
    <element name="enableUserRequest" />
    </schema>

    etc. somewhere.

    In any case - simply use an expression that selects the message nodes and then use the org.w3c.dom.Node interface to navigate to the parts you want. getAttributes() will give you a org.w3c.dom.NamedNodeMap and getChildNodes() will get you another org.w3c.dom.NodeList.
    [ September 28, 2007: Message edited by: Peer Reynders ]
    vjy chin
    Ranch Hand

    Joined: Feb 17, 2005
    Posts: 279
    Thanks for the reply. I tried to get the values, but not able to get the type part (I need to get xsd:string as result) in


    The code I have now is



    I am not able to get the value. Am I missing something?

    Thanks for your help.

    P.S. I tried WSDL4j library and I was able to get the results. But the value I get is some ibm class (com.ibm.wsdl.ServiceImpl), so I am not sure if it would be good.
    [ September 28, 2007: Message edited by: vjy chin ]
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    Your WSDL sample uses namespaces while your XPath expression does not ... ???

    This (non-namespace) code sample works fine:


    Output:
    vjy chin
    Ranch Hand

    Joined: Feb 17, 2005
    Posts: 279
    Thanks Peer,

    It worked great and I changed slightly according to my requirement.

    Thanks once again for your time and help.
    Elsan
    Greenhorn

    Joined: Jan 29, 2002
    Posts: 1
    Hi,

    You can use axis's Emitter class which is being used by axis's WSDL2Java behind the scene. This is same as WSDL2Java but you can use it programmatically.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60800
        
      65

    Elsan, please check your private messages for an important message.


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    Originally posted by Elsan:
    You can use axis's Emitter class which is being used by axis's WSDL2Java behind the scene.


    The point that I made earlier in the thread was that the WSDL is the service contract. The output from WSDL2Java is merely a translation of the original service contract according to certain mapping rules. That translation is used a convenience during development but should never be mistaken for the actual service contract.
    pavitra jayaraman
    Greenhorn

    Joined: Oct 10, 2007
    Posts: 10
    hey i am an new bee to web services and i need to perform this task and can you please suggest some web sites where i can learn them in detail....thanks in advance
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: WSDL2Java Question
     
    Similar Threads
    Exception handling!
    Consuming Webservice in Java
    WSDL access error
    Axis 1.4 Error plz help Connect Exception
    Exception thrown While Running a Client