How to write JAX-WS WebServices and WebService Client in Java

Today, I am going to explain about how to write JAX-WS Webservice providers(Endpoints) and how to create webservice client which uses published webservices.

JAX-WS is Java Api for XML webservices. APIs for creating webservices in XML formats.

Let's consider that I want to write JAX-WS Service which will publish customer Information and client will use this service and display requested customer Information.

You require to develop following parts :
  1. Create WebService Endpoint Interfaces
  2. Create WebService Endpoint Implementation
  3. Endpoint Publisher
  4. Analyze Created wsdl file
  5. Create WebService Client which will use exposed services. - CustomerService Interface

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.anuj.rep.Customer;

public interface CustomerService {

 @WebMethod public Customer getCustomerInfo(String customerId);
} - Implementation Logic for CustomerService Interface

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.anuj.endpoints.interfaces.CustomerService;
import com.anuj.rep.Customer;

public class CustomerServiceImpl implements CustomerService {

 private List customers = new ArrayList();
 public CustomerServiceImpl(){
 public Customer getCustomerInfo(String customerId) {
  Customer customer = null;
  for(Customer cust : customers){
    customer = cust;
  if(customer != null){
   return customer;
   return null;

 protected void createCustomers(){
  Customer customer = new Customer();

Analyze Created WSDL File
Once WebService is publised, You can access WSDL(Web Service Description Language) using Url  http://localhost:8080/webservices/customers?wsdl

Create WebService Client
wsimport command is used to parse wsdl file and generate requires files for accessing published JAX-WS Service.

wsimport -keep http://localhost:8080/webservices/customers?wsdl

Once all required files has been generated, you can access exposed services as below :

import com.anuj.endpoints.implementation.CustomerService;
import com.anuj.endpoints.implementation.CustomerServiceImplService;
import com.anuj.endpoints.interfaces.Customer;

public class JAXRSClient {

  * @param args
 public static void main(String[] args) {
  CustomerServiceImplService customerServiceImpl = new CustomerServiceImplService();
  CustomerService customerService = customerServiceImpl.getPort(CustomerService.class);
  Customer customer = customerService.getCustomerInfo("1");
  System.out.println("Customer Id : " + customer.getCustomerId());
  System.out.println("Customer Name : " + customer.getCustomerName());
  System.out.println("Customer Address : " + customer.getAddress());
  System.out.println("Customer Phone : "+ customer.getPhone());  

Output :
Customer Id : 1
Customer Name : Anuj
Customer Address : India
Customer Phone : 1234567890 

Hope you liked this tutorial of Creating JAW-WS WebServices Endpoints and WebService Clients.

No comments:

Post a Comment