Today, i am going to explain simple JAX-WS web service( based on SOAP) for the beginners who are new to web service development.
Web
Services can be divided into two categories.
1.
SOAP based web services
2.
RESTful web services.
JAX-WS
supports for both SOAP based and RESTful web services.
In this post, we are going to discuss about the SOAP based web
services. AS an entry point for the SOAP web service, first we will
discuss the architecture of the typical SOAP based web service.
Here
you can see that the SOAP client will communicate with the SOAP
service with the aid of the SOAP libraries. SOAP libraries will be
responsible to send and receive request and response between client
and server. Here the both request and response will be the SOAP
documents and those will be exchanged between client underlying SOAP
libraries and the Service underlying SOAP libraries. These SOAP
libraries will do the marshalling and unmarshaling whenever required.
What
is marshalling and unmarshalling?
Marshalling
– converting Java objects into the XML files.
Unmarshalling
- converting XML files into the Java objects.
JAX-WS
uses JAXB (Java Architecture for XML Binding) for the marshalling
and unmarshalling.
In this example, we are going to look at the simple SOAP based
web service that prints the hello concatenating with the provided
web service input parameter as the output. Refer the below project
tree structure to identify the components associated with the
web service.
I will briefly explain the web service concepts by getting the
examples from above sample web service.
Service Endpoint Interface (SEI) – HelloWorld.java
Service Implementation Bean (SIB) – HelloWorldImpl.java
Web Service Publisher - HelloWorldPublisher.java
Then we need to look at SEI, SIB and WsPublisher in detail.
SEI : - Service Endpoint Interface
first of all you need to know what is the SEI. SEI is just a Java interface
that defines/declares all the web service methods. In this example,
the following file will be the SEI.
HelloWorld.java
package com.jax.ws.samples;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* <p>
* this will be the SEI that declares all the web service methods
* </p>
*/
@WebService
public interface HelloWorld {
@WebMethod
String printWelcomeMessage(String username);
}
SIB :- Service Implementation Bean
SIB is the implementation of SEI. That means in the SIB, all the
web methods declared in the SEI will be implemented.
SIB can be implemented as two ways.
1. POJO class
2. Stateless Session EJB
In this example will be using POJO class as SIB to implement SEI as follows.
HelloWorldImpl.java
package com.jax.ws.samples;
import javax.jws.WebService;
/**
* <p>
* this will be the SIB that implements all the web service methods
* declared in the SEI.
* </p>
*/
@WebService(endpointInterface = "com.jax.ws.samples.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public String printWelcomeMessage(String username) {
return "Hello "+username;
}
}
as you can see, the end point interface will be the relevant java
interface (SEI) that is implemented by the relevant SIB.
Publishing the Web Service
once the SEI and SIB have been completed and compiled, the web
service is ready to be published. In production mode,web service
should be published using the application servers such as Tomcat,
JBoss, WebLogic etc... but for the testing purpose it is possible to
publish the web service with the simple java program as follows.
HelloWorldPublisher.java
package com.jax.ws.samples;
import javax.xml.ws.Endpoint;
/**
* <p>
* this will be the sample web service publisher class
* whose responsibility to publish the given web service
* </p>
*/
public class HelloWorldPublisher {
public static void main(String []args){
//1st argument - web service publication URL
//2nd argument - instance of SIB
Endpoint.publish("http://localhost:6666/sayHello",new HelloWorldImpl());
}
}
you can publish the web service by running this simple program (HelloWorldPublisher.java).
After running the publisher program, it is time to check whether the
published web service is up and running. This can be done with web
browser by getting the related WSDL document as follows.
http://localhost:9999/sayHello?wsdl
I think this will be helpful for you to start your first Java web service development.
This sample web service is available in the following GitHub repository.
https://github.com/chathurangat/jax-ws-helloworld
I think this will give you an brief idea about how to develop web services with java.
Hope this will helpful for you
Thanks and Regards
Chathuranga Tennakoon
No comments:
Post a Comment