Tuesday, April 06, 2010

JDK 6.0 Light Weight HTTP Server

Using JDK 6.0 Light Weight HTTP Server

Inorder to resolve a strange scenario at my work, I was pushed to search for something which exposes the functions in my java app as a webservice without actually using a webserver. After a lot of Googling, I have realized that I was searching for something which is already in my pocket.

Light Weight HTTP Server in JDK 6.0

In short, if you have a java app, and want to quickly expose few methods as a WebService without the hassles of having a Web Server, this is the solution. All you need to do is

1.       Identify the Java Class which u want to expose as webservice

2.       Annotate with @WebService and other WS related annotations

3.       Write a small method to publish your java object as web service

Hello World!

Create a Java Class which you want to expose as WebService

TestService.Java

package test;

import javax.jws.WebService;

@WebService

public class TestService{

                public String echo(String message){

                return message;

}

}

Inorder to publish the above method as Service, all you need to do is,

PublishProxy.java

package test;

import javax.xml.ws.Endpoint;

public class PublishService{

                public void publish(){

                                //Endpoint.publish(URL to which you want to bind the service, instance of object to be invoked)

Endpoint.publish(“http://localhost:8080/TestService”,new TestService());

                }

}

Bangon! Launch your fav browser, open the url http://localhost:8080/TestService?WSDL, you will see a freshly baked WSDL for your Service, ready for consumption!

Posted via email from My Hello World!

No comments: