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.java - 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;

@WebService
@SOAPBinding(style=Style.RPC)
public interface CustomerService {

 @WebMethod public Customer getCustomerInfo(String customerId);
}

CustomerServiceImpl.java - 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;

@WebService(endpointInterface="com.anuj.endpoints.interfaces.CustomerService")
public class CustomerServiceImpl implements CustomerService {

 private List customers = new ArrayList();
 
 public CustomerServiceImpl(){
  createCustomers();
 }
 
 @Override
 @WebMethod
 public Customer getCustomerInfo(String customerId) {
  Customer customer = null;
  
  for(Customer cust : customers){
   if(cust.getCustomerId().equals(customerId)){
    customer = cust;
    break;
   }
  }
    
  if(customer != null){
   return customer;
  }
  else{
   return null;
  }  
 }

 protected void createCustomers(){
  Customer customer = new Customer();
  customer.setCustomerId("1");
  customer.setCustomerName("Anuj");
  customer.setAddress("India");
  customer.setPhone("1234567890");
  
  customers.add(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