Building a Basic Web service using JAX-WS

Web Service: A web service is a service which runs over XML Data exchange in the form of SOAP Request and SOAP Response.  The advantage we got over web service is the communication with different language platforms e.g. Java, .net, C++, PHP   etc.  Web services are playing a crucial part in SOA (Service Oriented Architecture) which is again a very broad term and one of the successful architecture accepted by many enterprise level applications today. Let’s end the definition part here and move now to Web Service development part. (Hope the intro have made you clear the basic concept of Web Services if not please do Google more on web services 🙂 ).

Develop a Simple Web Service

To develop a web service there are 2 types of approaches we can follow.

1.       Contract First Approach.

2.       Code First Approach

Contract First Approach: This approach works on the basis of WSDL, and require a very good knowledge of WSDL, XML, XSD.  Once you expertise the web services then you can apply some hand on this approach. For this post I will describe how to build a web service using 2nd Approach.


Code First
Approach

This approach starts with the very core level of code writing. I mean by writing a Java Class and defining all the properties of web services i.e. ports, operation, endpoints etc.

Lets Develop a Ping Web Service: This web service will simply return a greeting message with Current date and time.

First of All write a Java Class:

package com.webservice.sample;

import javax.jws.WebMethod;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

import java.util.Date;

@WebService(name=”PingServer”, serviceName=”PingServer”,portName=”PingServerPort”, targetNamespace=”com.webservice.pingserver”)

@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)

public class PingServer {

@WebMethod

public String getPingStatus(){

return “Hi!! I am active :” + new Date().getTime();

}

}

Description:

@WebService :

Name: This Defines a name of web service
serviceName: This Defines a name of web service Endpoint interface

portName: This defines the name of the port

targetNamespace: Define a namespace for your web service, if no name specified, Compiler will take a default name space in the reverse order of you package name.

@SOAPBinding

Style       =Defines a Soap Styel e.g SOAPBinding.Style.DOCUMENT,

Use     = Specify a SOAP Message format e.g SOAPBinding.Use.LITERAL, 

parameterStyle =defines how Web service request/reply messages are interpreted by a Web service provider/consumer. Quite simply, "wrapper" style tells the Web service provider that the root element of the message (also called "wrapper element") represents the name of the operation and it is not part of the payload. This also means that children of the root element must map directly to parameters of the operation's signature. The "non-wrapper" style (also sometimes called "bare"), does not make this assumption; in this case the entire message will be passed to the service operation. The reply message is handled in a similar way.

@WebMethod : This specify the method which is exposed to web service(operation)

[NOTE]
in case if you are passing any arguments to the web service method.
You can use @WebParam(name=”argumentName”) annotation to comply with the schema. Otherwise generic “in0”, “in1” names will be used in WSDL for input arguments.

Generating the Web Service Artifacts

Now we need to create the artifacts of the web services. There is tool called WSGEN, This tools reads the Service Endpoint interface   and generate the WSDL and XML Schema for the web service which needs to be published.

To Run the tool first of all compile the Web service which we have created above. And open the command console and move the compiled directory e.g “/bin” dir. And run the below command.

wsgen –cp .  com.webservice.sample -wsdl

Once you run the tool you will notice the creation of WSDL and XDS creation in the “/bin” directory.

Now Let’s publish the Web Service.

In this part we will use the EndPoint class to publish the web service using a lightweight web server.

public class TestPublisher {

public static void main(String[] args) {

Endpoint.publish(“http://localhost:8011/service/pingserver”, new PingServer());

}

Call the publish method to publish the web service using FQN of the web service URL and the Service endpoint interface.

Endpoint.publish(URL, Object);

Once you run the Publisher. Go to Run -> Internet Explorer

And open the URL


http://localhost:8011/service/pingserver

If you are able to see the WSDL, Time to cheer!!!!! now… As the Web service has been published successfully.

[Note] Quit the Publisher Class to stop that Lightweight Web Server.

I will explain how to write a web service client to test the web service in my Next Post. Please give your suggestions or feedback.


Thanks
R Vashi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s