How to write hello world Java Webservice client

Connect with

Oracle Java
In java, you can write your java webservice client by different ways. If you have wsdl, and its accessible, you can generate java client code on terminal using wsimport tool. If you are interested in, how to generate java webservice source code using wsimport? you can visit our another post, i.e. How to generate Java Webservice artifact using wsimport (on linux terminal or window).

1. Prerequisite to create java web services client

You must have installed following in your machine (laptop/desktop/server)

  • JDK 6 or latter version
  • IDE (Eclipse or any another)
  • javapath enrironment configured to access wsimport utility from folder
  • copy generate java webservice client artifact in your project
  • Your Hellow World java webservices up and running and ready to access

2. Wrting of java webservice client

File: HelloWorldWSClient.java

package com.mysoftkey.jaxws.client;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.mysoftkey.jaxws.hellowold.HelloWorldImpl;

/**
 * This is client side code to invoke webservice.
 * Generate java source code by using wsdl file and run the following coe.
 * 
 * @author Ranjeet Jha
 */
public class HelloWorldWSClient {
 public static void main(String[] args) {
  try {
   URL url = new URL("http://localhost:8080/HelloWorldWS/HelloWorldWS?wsdl");

   /*
    * 1st argument service URI, refer to wsdl document above 2nd argument is
    * service name, refer to wsdl document above
    */
   QName qname = new QName("http://hellowold.jaxws.mysoftkey.com/", "HelloWorldImplService");

   Service service = Service.create(url, qname);

   HelloWorldImpl hello = service.getPort(HelloWorldImpl.class);
   String response = hello.sayHelloWorld("Hi mysoftkey.com");
   System.out.println(response);

  } catch (Exception e) {
   System.err.println("exception caught while running webservice , msg : " + e.getMessage());
  }

 }
}

Console Output 


Hello world JAX-WS, Hi mysoftkey.com

Your comments are welcome to improve this post. Happy Learing 🙂


Connect with

1 thought on “How to write hello world Java Webservice client

Leave a Reply

Your email address will not be published. Required fields are marked *