A simple Web Service

We are about to create a simple Web Service in Java. The only requirement is Java SE 6.

At first, we clearly define the operation to be exposed.

public interface SimpleService {
   String sayHello();
}

Let’s declare it as a web service.

@WebService
@SOAPBinding(style=Style.RPC)
public interface SimpleService {
   @WebMethod
   String sayHello();
}

API: WebService, WebMethod, SOAPBinding, SOAPBinding.Style

So we have a web service with a single operation: sayHello. Moreover we choose the simplified RPC style. Let’s implement the operation.

public class SimpleServiceImpl implements SimpleService {
   public String sayHello() {
      return "Hello from Web Services!";
   }
}

We should provide the complete name of the service interface,

@WebService(endpointInterface="simple.SimpleService")
public class SimpleServiceImpl implements SimpleService {
   public String sayHello() {
      return "Hello from Web Services!";
   }
}

where simple is the package of SimpleService.

At this point, it is easy to publish the web service locally.

public class SimpleServicePublisher {
   /**
    * Publishes a Web Service locally.
    */
   public static void main(String[] args) {
      Endpoint.publish("http://127.0.0.1:9000/simple", new SimpleServiceImpl());
   }
}

API: Endpoint

Click on this link to test it: http://127.0.0.1:9000/simple?wsdl.
You will see an XML document in your browser. It is the notorious WSDL, the contract that describes in detail the available operations of our Web Service. Since it is language-neutral, we may built a client in any programming language.

In order to make a Java client, we first need to specify the URL.

URL url = new URL("http://127.0.0.1:9000/simple");

Then we need to specify the qualified namespace that consists of two things:

  1. The URI is the value of the @namespace attribute in WSDL.
  2. The local part is the value of the service[@name] attribute at the bottom of WSDL.
QName name = new QName("http://simple/", "SimpleServiceImplService");

Now we can create the service,

Service service = Service.create(url, name);

get our port,

SimpleService simple = service.getPort(SimpleService.class);

and interact with our Web Service. The complete code of the client follows.

public class SimpleServiceClient {
   /**
    * Connects to a Web Service.
    */
   public static void main(String[] args) throws Exception {
      // Specify the URL
      final URL url = new URL("http://127.0.0.1:9000/simple");
      // Specify the qualified name
      final QName name = new QName("http://simple/", "SimpleServiceImplService");
      // Create the service
      final Service service = Service.create(url, name);
      // Get the port
      final SimpleService simple = service.getPort(SimpleService.class);
      // Call an operation
      System.out.println(simple.sayHello());
   }
}

API: URL, QName, Service

Summary

The following classes are involved.

  • SimpleService and SimpleServiceImpl define the functionality that will be exposed as a Web Service.
  • SimpleServicePublisher publishes the Web Service locally.
  • SimpleServiceClient is a client of the Web Service.

Thanks.

Advertisement

10 Responses to A simple Web Service

  1. Nommi says:

    hmmmmmmm amazing i would try this stuff this is realy quick way to design web service… i using XFire JAX-WS and also Spring webservice

    but with annotation u realy make it simple both service and its client…
    realy thanx keep it up..

  2. Zeljko says:

    Thanks Nikos for this excellent yet clarifying example and sharing knowledge. Great blog :-)

  3. [...] the previous post we created a simple Web Service in Java. Now we’ll build on it, by providing more [...]

  4. Alex Rouillard says:

    Thx a lot, EXACTLY what i was looking for :D

  5. Dennis says:

    Was a step missed? The code given is clear enough. However, it’s not obvious if something is deployed or if a server is involved. Is something packaged?

  6. Dennis says:

    Doh…I see these are run as java apps.

  7. bill perlman says:

    thanks for this post. It was an excellent example. I’m starting to read the other posts now.

  8. harry says:

    I am getting the following error when i try to invoke the service
    Exception in thread “main” javax.xml.ws.WebServiceException: {http://simple/}SimpleServiceImplService is not a valid service. Valid services are: {http://webservice/}SimpleServiceImplService
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:220)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:165)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)
    at javax.xml.ws.Service.(Service.java:56)
    at javax.xml.ws.Service.create(Service.java:680)
    at webserviceclient.SimpleServiceClient.main(SimpleServiceClient.java:19)

  9. harry says:

    I found the issue it was mybad, i gave a different namespace when i created the WS and was using the name from your sample client,

    Thank you for the simplest explanation of the WS creation steps.

  10. harry says:

    Hello,
    Could you post a similar post for other type of web services please.

    Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.