How to write a Java client to access WSDL file?

14,919

Solution 1

In addition to The Elite Gentleman's answer, here are my steps I successfully used to generate classes to be able to use the webservice: Command:

wsimport -Xnocompile -keep -b binding.xml wsdlFile.wsdl

Explanation:

  • '-Xnocompile' suppresses the generation of .class files
  • '-keep' makes sure the generated Java files wont be deleted (by default, only the .class files remain)
  • '-b ' specifies a binding configuration file. This is necessary! (see below)

I had the problem that the Java classes contained the JAXBElement<Type> wrapper classes. So instead of a class member of type String, I would get the type JAXBElement<String>, which is horrible to use. With the -b switch for wsimport and the following binding.xml file, you get the correct types:

<jaxb:bindings version="2.0"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jaxb:bindings>
        <jaxb:globalBindings generateElementProperty="false" />
    </jaxb:bindings>
</jaxb:bindings>

I hope this helps. wsimport then generates all the classes you need as well as a class containing methods for all your webservices' methods.

By default, these methods don't have a read timeout (talking network problems while requesting...), see here for a question I had back then.

Solution 2

You will need to need to generate a Java proxy from the WSDL File. You can do this by using Apche CXF or Apache Axis/Axis2 to generate Java Proxy/Java Client.

In Java 6, you can also generate java client too. On the JDK/bin there's wsimport to generate Web Service client or in Axis, there's WSDL2Java that does the same thing like wsimport.

Solution 3

I would take a look at getting your IDE to automatically generate everything.

In Netbeans, the steps are:

Right Click on your project, click "add Web Service Client", enter the WDSL url and click Finish.

This will auto-magically create the Java proxy for you.

To implement the client in your code, drag and drog the required method (located in Web Services References in your project), into your code.

Solution 4

Use wsimport -keep which will give you all the classes. Then write a different service class and copy all the method from the generated service class. Pass your own service class to wsgen -keep -p.It will give you the needful wrapper classes(jax-ws) which you are going to return from your delegate invocation methods.

Solution 5

I would recommend starting with the Web Service Explorer in Eclipse Java EE. This allows you to investigate any web service given the WSDL.

The instructions to convert the WSDL to Java depends on which web service library you want to use. If you use Java 6 the Metro stack is built in.

Share:
14,919
sarah
Author by

sarah

Updated on June 22, 2022

Comments

  • sarah
    sarah almost 2 years

    How can I access the exposed methods in a .wsdl file using Java? Also, what are the steps involved in writing a Java client and consuming the webservices?