Monday, January 19, 2009

Creating WebService in JDK6.0

Hi
In this session I will describe how to create a web service in Jdk 6.0

In the current release of Java (i.e JDK6.0 ) they have provided the web services stack with in the jdk. You can find the corresponding web services classes in rt.jar under the location %JAVA_HOME%\jre\lib\rt.jar.

Now, What does Web service stack mean?
web service stack is responsible to define, locate, implement, and make Web services . When we are creating a web service & deploying that in any app server then this web services stack takes care of everything w.r.t the corresponding web service.

Since this stack is available in JDK itself so we don't require any app server to deploy our web service. Let's start with a small example.

Let's call our WebService class as SampleService

package service.sample;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.EndPoint;

@WebService /*this annotaion tells our compiler that this class will be treated as Web Service class*/
public class SampleService{
@WebMethod /*this annotaion tells our compiler that this method is the web service method which will be exposed via http */
public String greetHallo(String consumerName){
return "hi "+ consumerName + "you have successfully consumed this service";
}
}

Now our class is ready which can be used as a web service.
Next step is to compile our web service class & make it ready. So, lets go further to compile it. Before that there is one thing which we have to consider i.e. the use of annotation in our class. So, to generate the wrapper classes for the corresponding annotations, we will use the APT(apt) tool. Which stands for Annotation processing tool. It comes with JDK from 5.0 version onwards.

lets use the apt tool to compile our class & generate the wrapper classes.

  • apt -d ws SampleService.java (-d option will keep the generated class files inside the specified directory. In our case its ws)
Now the next job is to publish our web service thorugh an endpoint url. lets write our java class to publish our web service.

package service.publish;

import javax.jws.EndPoint;
import service.ws.*; /*the class files we have generated through apt */

public Class PublishService{

private static String ENDPOINT_URL = "http://localhost:12546/sample";
/*the end point url consists of a valid hostname where we want to run our web service, a port number & a name by which the web service will be identified*/

public static void main(String x[]){

/*Create an instance of our webservice class*/
SampleService sampleService = new SampleService();

/*publish the service through endpoint*/
EndPoint endPoint = EndPoint.publish(ENDPOINT_URL ,sampleService);
}
}

Next step is to compile & run our PublishService class.

> javac PublishService.java
>java -cp ws PublishService /*keeping our apt generated wrapper classes in classpath*/

with this our web service is running on the machine localhost on the port 12546 & in the name of sample. We can get the WSDL for our service by accessing the url

http://localhost:12546/sample?wsdl

This service can be consumed by varoius ways.There are availability of lots of client tools to consume the web service. in jdk 6 there is a command line tool called wsimport to consume any web service.

3 comments:

  1. Thanks Abani for giving us a brief knowledge about
    Web service creation.Can you give some knowledge about wsimport tool also,because I am really interested to know about it after reading this post.And thanks again for taking a step ahead in this.Keep it up...& All the best:)

    ReplyDelete
  2. Good one Abani. Thanks by Samy

    ReplyDelete
  3. Abani - I read, this webservice is served by light-weight HTTP Server which comes inbuilt with jdk 6. My doubt is how do we make this webservice as unavailable to client for the short term? Is un-deployment the only option?

    ReplyDelete