Add a web service to a already available Java project

Based on the article I linked in the comments above :: http://www.ibm.com/developerworks/webservices/tutorials/ws-eclipse-javase1/index.html

With the JWS annotations you can setup a Web Service in your java application to expose some of its functionality. There is no extra libraries needed. The below examples were written with Java 6.

An example of Defining your web service :

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

@WebService
public class MyWebService {

    @WebMethod
    public String myMethod(){
        return "Hello World";
    }

}

Note the 2 annotations of @WebService and @WebMethod. Read their API’s which are linked and configure them as required. This example will work without changing a thing

You then only need to setup the Listener. You will find that in the class javax.xml.ws.Endpoint

import javax.xml.ws.Endpoint;

public class Driver {

    public static void main(String[] args) {
        String address = "http://127.0.0.1:8023/_WebServiceDemo";
        Endpoint.publish(address, new MyWebService());
        System.out.println("Listening: " + address);

    }
}

Run this program and you will be able to hit your web service using http://127.0.0.1:8023/_WebServiceDemo?WSDL. At this point it is easy to configure what you want to send back and forth between the applications.

As you can see there is no need to setup a special web service project for your use.

Leave a Comment