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
-
Create a new web project in Eclipse or MyEclipse, then add a new class that will be published.
-
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
}
}
-
Compile and run the main method (check JDK version if any error occurs).
-
Append
?wsdlto the endpoint address and open it in a browser. If an XML document appears, the service has been published successfully.
2. Invoking the Web Service
Approach 1: Generate Client Code via IDE
-
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) -
Download the generated artifacts (ensure the JDK version matches the service's JDK).
-
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);
}
}
Approach 2: Generate Code Using wsimport
-
Create a Web Service Project (e.g., named TheClient).
-
Open a command prompt and run the
wsimporttool: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 -
A successful execution outputs:
parsing WSDL…generating code…compiling code…
-
Refresh the project; the generated classes will appear.
-
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, packagejavax.jws.WebService). - Publish it using
Endpoint.publish()(classjavax.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
@WebServiceannotated class are published by default. staticandfinalmethods 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.