Sunday, 4 March 2007

Web Services with Java SE 6

One of the most interesting features of Java SE 6 is the new new end-to-end web services stack - JAXWS 2.0 FCS RI. The new runtime offers an embedded simple web server which can be used for deploying web services. Coupled with the embedded Derby DB JavaSE 6 become a complete platform.

Here is a simple example, lets start with a POJO annotated with @WebService annotation. This annotation tells JAXWS that its a Web Service endpoint. You may also annotate the methods that will be exposed as web services method with @WebMethod annotation. If all the methods will be exposed as web services method, it is not needed to annotate each method.

package com.muratyener;
import javax.jws.WebService;import javax.jws.WebService;

public class Service {

public String helloWorld(String message) {
return "Hello "+ message + "!!!";
public static void main(String[] args){
// create and publish an endpoint
Service service = new Service ();
Endpoint endpoint = Endpoint.publish(http://localhost:8080/service, service);


Well thats it, now we have to do two things to deploy our service.

To compile and generate wrapper classes;
apt -d sample com/muratyener/

To publish the service;
java -cp sample com.muratyener.Service

Now, lets try accessing our deploed WSDL by typing http://localhost:8080/service?wsdl. JAXWS internally provides all the required stuff using light-weight HTTP server.
It is fast and simple, a very nice improvement.