Creating and Invoking Java Web Services: A Step-by-Step Visual Guide

Overview of Web Services

Web Service technology enables applications running on different machines to exchange data or entegrate without additional, specialized third-party software or hardware. Applications built according to Web Service standards can communicate regardless of their programming languages, platforms, or internal protocols. A Web Service is a self-describing, self-contained network module that executes specific business functions. They are easy to deploy becuase they rely on established industry standards and existing technologies, such as XML (a subset of the Standard Generalized Markup Language) and HTTP. Web Services reduce the cost of application interfaces and provide a universal mechanism for integrating business processes across an enterprise or between multiple organizations.

1. Creating a Web Service

  1. Create a new web project in Eclipse or MyEclipse, then add a new class that will be published.

    Create a new class for the Web Service

  2. Write the methods that client applications will call.

    Example:

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
 * Demonstrates publishing a web service using the javax.jws API (JDK 1.6+).
 * @WebService annotation marks this class as a web service endpoint.
 * Endpoint publishes an object annotated with @WebService at a given address.
 */
@WebService
public class GreetingService {

    /**
     * A callable service method that returns a personalized greeting.
     * @param visitor Name of the visitor
     * @return A welcome message
     */
    public String greet(String visitor) {
        return "Welcome! " + visitor;
    }

    /**
     * This method will NOT be published because of exclude=true.
     * @param name
     * @return
     */
    @WebMethod(exclude = true)
    public String sayHi(String name) {
        return "Hi! " + name;
    }

    /**
     * Static methods are not exposed as web service operations.
     * @param name
     * @return
     */
    public static String farewell(String name) {
        return "Goodbye! " + name;
    }

    public static void main(String[] args) {
        // Publish the web service at the specified endpoint address.
        Endpoint.publish("http://192.168.1.105:8080/Service/GreetingService", new GreetingService());
        System.out.println("Service published successfully!");
        // After publishing, access the WSDL at:
        // http://192.168.1.105:8080/Service/GreetingService?wsdl
    }
}
  1. Compile and run the main method (check JDK version if any error occurs).

    Compile and run the service class

  2. Append ?wsdl to the endpoint address and open it in a browser. If an XML document appears, the service has been published successfully.

    WSDL output in browser

2. Invoking the Web Service

Approach 1: Generate Client Code via IDE

  1. Create a new class for calling the web service. Right-click the src folder, choose Web Service Client, enter the WSDL URL, and select a download path for the generated code.
    (WSDL URL: http://192.168.1.105:8080/Service/GreetingService?wsdl)

    Step 1: Create Web Service Client

    Step 2: Specify WSDL and output folder

    Step 3: Client code generation complete

  2. Download the generated artifacts (ensure the JDK version matches the service's JDK).

  3. Write a caller that invokes methods of the generated service class.

    Example:

import com.demo.ws.greet.GreetingService;
import com.demo.ws.greet.GreetingServiceEndpoint;

/**
 * A client for invoking the GreetingService web service.
 */
public class ServiceClient {

    public static void main(String[] args) {
        // Call the web service
        GreetingService service = new GreetingServiceEndpoint().getGreetingServicePort();
        String reply = service.greet("Alex");
        System.out.println(reply);
    }
}

Client invocation output

Approach 2: Generate Code Using wsimport

  1. Create a Web Service Project (e.g., named TheClient).

  2. Open a command prompt and run the wsimport tool:

    Syntax: wsimport -s "source_directory" -p "target_package" -keep "wsdl_URL"

    Example: wsimport -s G:\workspace\webService\webService_Project\src -p com.demo.ws.greet -keep http://192.168.1.105:8080/Service/GreetingService?wsdl

  3. A successful execution outputs:

    • parsing WSDL…
    • generating code…
    • compiling code…
  4. Refresh the project; the generated classes will appear.

  5. Write a client call exactly as shown in Approach 1.

3. Summary

To publish a Web Service:

  • Annotate the class with @WebService (available since JDK 1.6, package javax.jws.WebService).
  • Publish it using Endpoint.publish() (class javax.xml.ws.Endpoint). This method accepts the local service address and an instance of the annotated class.

Key rules:

  • All non-static methods of an @WebService annotated class are published by default.
  • static and final methods cannot be published.
  • Annotating a method with @WebMethod(exclude=true) prevents it from being published.

A sample project is available at: https://github.com/xuwujing/webservice_project.

Tags: java WebService JAX-WS wsimport Endpoint

Posted on Tue, 14 Jul 2026 16:20:31 +0000 by mikelmao