开发者

How to write a Java client to access WSDL file?

How can I access the exposed methods i开发者_如何学编程n a .wsdl file using Java? Also, what are the steps involved in writing a Java client and consuming the webservices?


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.


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.


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.


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.


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.


soapUI is one of the softwares allowing you to easily get into WSDL / SOAP world. You can generate SOAP requests with test values to be sent to the server. You can also see the server's SOAP responses. This will let you understand a bit more of WSDL / SOAP. For generating the java code from the wsdl see the recommendations above.


This is now simple when using your IDE. Lets take netbeans as our sample.

  1. Open netbeans
  2. create a new project by clicking file and choose new project
  3. choose a java under category and java-application under applications
  4. right click your newly created project and choose new and choose web service client.
  5. Netbeans will ask for a WSDL and client location

    [wsdl and client location][1]

      [1]: https://i.stack.imgur.com/yWQZv.png
    
  6. choose WSDL and enter the URL to the of the WSDL location

  7. click finish and all methods will be created for you

Your task from here is to call the methods and pass the parameters

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜