SOAP

JAX WS Hello World Example

In this tutorial we want to run a JAX-WS example (Endpoint + Client) for beginner of JAX-WS web service give the few minutes to this tutorial and happy learning ; ) .
Prerequisites:
  1. JDK 1.6
  2. Eclipse (or STS) IDE

Developing WebService End Point

1) Open Eclipse (or STS), and create a java project “JAXWSServerHello“.

2) Create WSServiceHello Endpoint Interface:

WSServiceHello .java

package com.dineshonjava.ws;

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

/**
 * @author Dinesh Rajput
 *
 */
@WebService
public interface WSServiceHello {
 
 @WebMethod String sayHello(String name);
 
}

3) Create WSServiceHello Endpoint Implementation class:
WSServiceHello .java

package com.dineshonjava.ws;

import javax.jws.WebService;

/**
 * @author Dinesh Rajput
 *
 */
@WebService(endpointInterface="com.dineshonjava.ws.WSServiceHello")
public class WSServiceHelloImpl implements WSServiceHello {

 @Override
 public String sayHello(String name) {
  return  "Hello, Welcome to JAX-WS World and keep learning!! " + name;
 }

}

4) Create Endpoint Publisher class:
WSHelloPublisher.java

package com.dineshonjava.publisher;

import javax.xml.ws.Endpoint;

import com.dineshonjava.ws.WSServiceHelloImpl;

/**
 * @author Dinesh Rajput
 *
 */
public class WSHelloPublisher {

 public static void main(String[] args) {
  Endpoint.publish("http://localhost:8181/WS/WSServiceHello"
    ,new WSServiceHelloImpl());
 }

}

5) Run above program.Your web service is published.You can check your service wsdl at http://localhost:8181/WS/WSServiceHello?wsdl
WSServiceHello.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.dineshonjava.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.dineshonjava.com/" name="WSServiceHelloImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://ws.dineshonjava.com/" schemaLocation="http://localhost:8181/WS/WSServiceHello?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="WSServiceHello">
<operation name="sayHello">
<input message="tns:sayHello"/>
<output message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="WSServiceHelloImplPortBinding" type="tns:WSServiceHello">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="WSServiceHelloImplService">
<port name="WSServiceHelloImplPort" binding="tns:WSServiceHelloImplPortBinding">
<soap:address location="http://localhost:8181/WS/WSServiceHello"/>
</port>
</service>
</definitions>

Developing WebService Client :
1) Open eclipse(or STS) and create a new java project “WSHelloClient”

2) As you know we need to generate the client stubs… but how?
open your command line, and enter the wsimport command:

CD %CLIENT_PROJECT_HOME%src
wsimport -keep http://localhost:8181/WS/WSServiceHello?wsdl

you will find java classes generated and compiled under src->com->dineshonjava->ws


3) Lets create client class now.
create WSHelloClient.java under src->com.dineshonjava.client

package com.dineshonjava.client;

import com.dineshonjava.ws.WSServiceHello;
import com.dineshonjava.ws.WSServiceHelloImplService;

/**
 * @author Dinesh Rajput
 *
 */
public class WSHelloClient {

 public static void main(String[] args) {
  WSServiceHelloImplService service = new WSServiceHelloImplService();
  WSServiceHello serviceHello = service.getWSServiceHelloImplPort();
  System.out.println("***************Call Started*******************");
  System.out.println(serviceHello.sayHello("Dinesh"));
  System.out.println("***************Call Ended*********************");
 }

}

4) Run above program and you will get following output.

Congratulation, you have successfully created web service endpoint and client. Now in next post, we will deploy it on Tomcat.

Download Source Code 
SourceCode.zip

References
Wikipedia for Web Service

 

<<Previous <<   || Index ||   >>Next >>

 

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago