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.

public interface SimpleService {
   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,

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("", new SimpleServiceImpl());

API: Endpoint

Click on this link to test it:
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("");

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("");
      // 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

API: URL, QName, Service


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.



23 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 😀

  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:

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

    Thank you.

  11. Bio says:

    Do you need a webserver? If yes, what webserver should I use? Sorry to bother you (I have a PHP server that needs to call Java).


  12. oramirez says:

    Finally! A Simple web Service tutorial 🙂

  13. atulshinkar says:

    Great post Nikos 🙂
    Really appreciate the simplicity in the explanation you provided.

  14. madan712 says:

    Thanks a lot… I was serching similar code since long time. Finally i found it here. It gave an excellent start in webservice. Thanks again.

  15. nucleo90 says:

    Thanks for the tutorial. That’s exactly what I was looking for.
    But I have a little problem: since the only requirement for this web service is Java 6, I assume that the webserver is Sun Java System Web Server. But how do I demonstrate this? I created the project in Eclipse and I don’t have any bulid.properties file where I think it should be set the VS.DIR property referencing the webserver location. Thanks again.

  16. Avinash Srivastava says:

    Thanks Nikos. What a great post it is? This is the best compact tutorial I have ever seen in my life.You are great Dear.

  17. Robert says:

    Great post! Know I just need to figure out how to publish something like this to my server!

  18. Sai says:

    Thanks, a very simple and clear example…

  19. Paulo says:

    Thanks. Very nice.

  20. espen says:

    I get this error: Exception in thread “main” com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException: 2 counts of InaccessibleWSDLException.

    java.net.ConnectException: Connection refused: connect
    java.net.ConnectException: Connection refused: connect

    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(Unknown Source)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
    at javax.xml.ws.Service.(Unknown Source)
    at javax.xml.ws.Service.create(Unknown Source)
    at com.simple.webservice.SimpleServiceClient.main(SimpleServiceClient.java:18)

  21. BootCat says:

    Thank you , This is very helpful . A full working example of creation of a service in a single page.

    Other web pages are crap . i need to browse 5 pages for this.

  22. Avinash Srivastava says:

    Really nice post. If anybody has a problem related to not generating the WSDL file in above example then there is a simple step you have left. Just execute the ‘SimpleServicePublisher.java’ before trying to generate the WSDL. It will actually define the endpoint where you can be searched while running the client.

  23. Good post. I will be facing some of these issues as well.

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 )

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: