Invoking the Web service interface using Wsimport and JAX-ws

Source: Internet
Author: User
Tags abstract definition wsdl

This article provides a simple example of how to invoke the Web service interface using the Wsimport tool and the JAX-WS API.
Advantages of this approach: use of the JDK's own tools and API interfaces, without the need to rely on third-party libraries.


JDK version: 1.8.0_141
Development tools: Eclipse

Server source code: Download
Client source code: Download


1. Generate a Web service client stub based on WSDL using the Wsimport tool that comes with the JDK
1.1. Ensure that the JDK1.6 version or a later version is installed
1.2. Ensure that the WebService service is activated
1.3. Run the following command at the command line:


Parameter description:
-D Specify the save path (. class file) of the build output file, and decide whether to generate a class file as needed
-s Specifies the save path (. java file for the generated Java source file, and optionally determines whether to generate Java source files)
-p Specifies the package name of the generated Java class
Http://localhost:8888/HelloService represents the WebService URL address, which must be added after the URL address. WSDL "parameter. The WSDL parameter can also be lowercase (WSDL).
To see what actions the service provides specifically, add the following URL. Tester "parameter. (sometimes passing tester does not necessarily return a result.) This is related to how the service is published)
1.4. View the generated files
, the exported file is guaranteed to exist in the path (jaxws\client\stub) that is made up of the specified package name.

2. Import the generated client stub class into the Eclipse project.

3. Writing client code
Create the jaxws.client. Helloappclient class.
The client code is shown in the following:

 Packagejaxws.client;Importjava.io.IOException;Importjava.net.MalformedURLException;ImportJava.net.URL;ImportJavax.xml.namespace.QName;ImportJavax.xml.ws.Service;ImportJaxws.client.stub.HelloService; Public classhelloappclient { Public Static voidMain (string[] args) {SayHello (); }     Public Static voidSayHello () {URL URL=NULL; Try{URL=NewURL ("Http://localhost:8888/HelloService"); } Catch(malformedurlexception e) {e.printstacktrace (); } Service S= Service.create (URL,NewQName ("http://service.jaxws/", "Helloserviceservice")); HelloService HS= S.getport (NewQName ("http://service.jaxws/", "Helloserviceport"), HelloService.class); String HelloMessage= Hs.sayhello ("Tom");                System.out.println (HelloMessage); Try{System.in.read (); } Catch(IOException e) {e.printstacktrace (); }    }}

Note: The port and network port (ports) mentioned in WebService are not a concept.
Some simple conceptual explanations in the following WSDL document (see WSDL standard If you want to learn more):

a WSDL document defines services as collections of network endpoints, or ports . In WSDL, the abstract definition of endpoints and messages are separated from their concrete network deployment or data for Mat bindings. This allows the reuse of the abstract definitions: messages , which is abstract descriptions of the data bein G exchanged, and port types which is abstract collections of operations . The concrete protocol and data format specifications for a particular port type constitutes a reusable binding. A port is defined by associating a network address with a reusable binding, and a collection of ports define a service. Hence, a WSDL document uses the following elements in the definition of network services:

    • Types –a container for data type definitions using some type Syste m (such as XSD).
    • Message –an Abstract, typed definition of the data being communicated .
    • operation –an Abstract description of an action supported by the Service.
    • Port Type –an Abstract set of operations supported by one or more Endpoi NTS.
    • Binding –a Concrete Protocol and data format specification for a particular port type.
    • Port –a single endpoint defined as a combination of a binding and a NETW Ork address.
    • Service –a collection of related endpoints.

Code Description:
(1) The URL represents the WebService service address. This address can be added later or not added "? WSDL "parameter.
(2) Service provides client view of WebService services. It acts as a client agent for endpoint, which is used to distribute (dispatch) calls to remote service operations (operation) (which, in layman's words, sends a method call on a service to the service serving on a remote server).
(3) HelloService represents the WebService service client stub (similar to a WebService service's image on the client for the client code to call its interface).
(4) Invoking the service interface through the Hs.sayhello method.

Invoking the Web service interface using Wsimport and JAX-ws

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.