WebService WSDL Generation Service

Source: Internet
Author: User
Tags wsdl

Because the previous example is a server that is published under the current project, it is also a server-published webservice that is accessed under the current project. But in practical applications, our services are often separated from the customer 諯, and even they are written by different people in different projects. And like the previous interface classes that use the service side in the current project: Imyservice is not very realistic. Because at this point you only have the service side of the WSDL, how can directly use the Server interface class to receive and test it. How to solve this problem?

The JDK's Bin directory comes with a tool, Wsimport. A webservice used to export the east, the server can be exported to generate the jar file, and then the client reference is not the line?

OK, let's see how to use it below.

It has several very important parameters,

-D indicates the output directory, the directory must exist beforehand, or the export fails.

-keep indicates whether the source code Java file is also exported when exporting the WebService class file.

The-verbose represents the detailed information.

Look at our export command. We are directly in the D drive.

[Plain]View Plaincopyprint?
    1. Wsimport-d d:-keep-verbose http://localhost:7777/tudou?wsdl

We exported files directory is D:\com\whaty\platform\ws\server This directory everyone has not found, just like our previous server-side directory is the same, but in fact in the WSDL namespace is the opposite of HTTP/ server.ws.platform.whaty.com/. In other words, the server generates the WSDL in turn, and the Wsimport is namespace when generated by the WSDL.

We directly put the generated directory in the 2nd built project. This way our client's project can be imyservice directly using the interface of the server. By the way, we have copied the client access class from the previous chapter and tested it. The directory structure reads "Note This is a new project":

Run the myclient directly to test it.

[Java]View Plaincopyprint?
  1. Package com.whaty.platform.ws.client;
  2. Import java.net.MalformedURLException;
  3. Import Java.net.URL;
  4. Import Javax.xml.namespace.QName;
  5. Import Javax.xml.ws.Service;
  6. Import Com.whaty.platform.ws.server.IMyservice;
  7. /**
  8. * @className: Myclient.java
  9. * @Desc: Access to published services
  10. * @author: Lizhuang
  11. * @createTime: 2012-12-21 01:23:57
  12. */
  13. Public class MyClient {
  14. public static void Main (string[] args) {
  15. try {
  16. //Address of the service WSDL document
  17. URL url = new URL ("http://localhost:7777/tudou?wsdl");
  18. //1.qnameqname is a shorthand for qualified name
  19. //2. Composition: Consists of a namespace (namespace) prefix (prefix) and a colon (:), and an element name
  20. //namespace is known as http://server.ws.platform.whaty.com/by the published WSDL,
  21. QName qname=New QName ("http://server.ws.platform.whaty.com/","Myserviceimplservice");
  22. Service service=service.create (URL, QName);
  23. Imyservice Ms=service.getport (imyservice.   Class);
  24. Ms.add (1, 4);
  25. Ms.minus (1, 4);
  26. } catch (Malformedurlexception e) {
  27. E.printstacktrace ();
  28. }
  29. }
  30. }


Console printing:

A+b=5
A-b=-3

See no, and before we in the service side test effect is the same, the difference is the file is not the same. The wsimport generated file folds The webservice of the service side into several files. Each method and request corresponds to 2 files respectively. You will find that there is a class of Myserviceimplservice:

[HTML]View Plaincopyprint?
    1. @WebServiceClient (name = "Myserviceimplservice", targetnamespace = "http/ server.ws.platform.whaty.com/", wsdllocation = " http://localhost:7777/tudou?wsdl ")
    2. public class Myserviceimplservice


This class is a well-annotated client program, which has a method:

[HTML]View Plaincopyprint?
    1. /**
    2. *
    3. * @return
    4. * Returns Imyservice
    5. */
    6. @WebEndpoint (name = "Myserviceimplport")
    7. Public Imyservice Getmyserviceimplport () {
    8. Return Super.getport (New QName ("http://server.ws.platform.whaty.com/", "Myserviceimplport"), Imyservice.class);
    9. }


Directly help us to generate a QName access program. Now that we don't have to write our own QName, it's better to test them directly.

The simplified client test method is as follows:

[Java]View Plaincopyprint?
  1. Package com.whaty.platform.ws.client;
  2. Import Com.whaty.platform.ws.server.IMyservice;
  3. Import Com.whaty.platform.ws.server.MyServiceImplService;
  4. /**
  5. * @className: Myclient2.java
  6. * @Desc: Get quick access to our services using the Myserviceimplservice generated by JDK Wsimport.
  7. * @author: Lizhuang
  8. * @createTime: 2012-12-22 12:24:37
  9. */
  10. Public class MyClient2 {
  11. public static void Main (string[] args) {
  12. Myserviceimplservice myserviceimplservice=New Myserviceimplservice ();
  13. Imyservice msis= Myserviceimplservice.getmyserviceimplport ();
  14. Msis.add (1, 4);
  15. Msis.minus (1, 4);
  16. }
  17. }


You will find the results are the same.

WebService WSDL Generation Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.