Saturday, 19 October 2013

A simple WebService using JDK

Objective

To develop a Soap WS in standalone Java (no web/app server).

Environment

  • JDK 1.7
  • Eclipse IDE

Development

  • Create a Eclipse Java Project and add a class definition as given below:
package test;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService(name = "Alarm", serviceName = "AlarmingService", portName = "AlarmPort")
public class AlarmingListenerImpl {

    @WebMethod
    public String sendAlarm(@WebParam(name = "alarm") String alarm) {
        if (alarm != null) {
            System.out.println("Message received:" + alarm);
        }
        return "success";
    }

    public static void main(String[] args) {
        // Start Alarm Listener
        Endpoint endPoint = Endpoint.create(new AlarmingListenerImpl());
        endPoint.publish("http://localhost:9001/alarmingService");
    }
}
  • Start the class (main method is written in the same class); this will be running and listening on port 9001 for any WS requests.

Testing

  • You need any WS client to test the WS you just created.
  • I used Eclipse Java EE Perspective's "Web Service Explorer" (Run -> Launch the Web Service Exporer):
  • You can also try JDK provided tool wsimport to generate WS Client classes and write a Java client.
Thanks You!