Publish a WebService from a POJA (plain old Java application) – that is: out of the container using EndPoint class

 

This very brief article will demonstrate a very useful feature of Java 6 Standard Edition – that means: the Java as it is running in any stand-alone application. It is possible – and even extremely simple as it turns out – to have a Java SE application publish a (SOAP) WebService. That is: without using a Web Server or Application Server, your Java application can receive WebService calls. I had heard about this but could not actually believe that I understood correctly. When I met Gerard Davison this week – a very nice and knowledgeable guy working on web services tools for the JDeveloper IDE – he confirmed and explained it to me. 

Through the EndPoint class – part of the JAX-WS implementation – we can instruct the JVM to start receiving SOAP calls on the URL we tell it to listen to and have those calls forwarded to our Java class. Thus it is very simple all of a sudden to for example provide mock implementations of services our BPEL process or ESB needs to invoke. This mechanism makes interoperability between for example .NET and Java very easy – especially in terms of the infrastructure required. And of course in situation where a Java application running outside any container make a call to an Asynchronous WebService – this is the basis of the answer to the question how the asynchronous response can be handed back to the application. See for example Gerard’s article on this: Calling Asynchronous WebServices from Java application. Let’s see how it actually works.  Create the simplest class in the world:

package nl.amis.ws;

public class UpperCaser {

    public String process(String text) {
        return text!=null?text.toUpperCase():"" ;
    }

    public static void main(String[] args) {
    }
}

Then either add the annotations for exposing the class as a WebService or use your IDE facilities to it for you. I of course did the latter – having JDeveloper publish this class as a WebService for me (that means: adding the required annotations).

package nl.amis.ws;

import javax.jws.WebService;
@WebService(name = "StringService", serviceName = "StringService", portName = "StringServiceSoapHttpPort")

public class UpperCaser {

    public String process(String text) {
        return text!=null?text.toUpperCase():"";
    }

    public static void main(String[] args) {
    }
}

Now we have a WebService that will be exposed when we deploy this application to our container. However, I also want to have the WebService exposed when I just run the main method of the application (well, the class). And that is where the EndPoint class comes in:

package nl.amis.ws;

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

@WebService(name = "StringService", serviceName = "StringService", portName = "StringServiceSoapHttpPort")
public class UpperCaser {

    public String process(String text) {
        return text!=null?text.toUpperCase():"";
    }

    public static void main(String[] args) {
     Endpoint e =  
           Endpoint.publish("http://localhost:7890/endpoint", new UpperCaser());      
     try{
            Thread.sleep(40*1000);
        }
        catch (Exception ex) {
           System.out.println(ex);
        }
    }

}

Using the static publish method on the EndPoint class, we specify the URL on which the WebService should be available and pass the class-with-WebService annotation to handle the calls received at that URL.

When I ask my IDE to run the class UpperCaser, it provides me with two options – one because of the @WebService annotation and one because of the main method:

Publish a WebService from a POJA (plain old Java application) - that is: out of the container using EndPoint class runapplication 

 Publish a WebService from a POJA (plain old Java application) - that is: out of the container using EndPoint class runaswhat

I elect to run the application as a stand alone Java application in the JVM without container whatsoever.

When the application is running, I can access it at the URL specified in the call to EndPoint.publish and for example retrieve the WSDL that is dynamically created by the JAX-WS framework – URL is http://localhost:7890/endpoint?wsdl:

Publish a WebService from a POJA (plain old Java application) - that is: out of the container using EndPoint class wdslforjavase

Calling the WebService is easily done using for example Soap UI. I create a new project, provide the URL of the WSDL and have SOAP UI prepare a request to call the WebService:

Publish a WebService from a POJA (plain old Java application) - that is: out of the container using EndPoint class thetestcalltojavassews

Then I have Soap UI make the call to the WebService and sure enough the response comes in:

Publish a WebService from a POJA (plain old Java application) - that is: out of the container using EndPoint class antwoordinsoapui

So it really was that simple. Create a class, provide @WebService annotation, write a main method to publish an end point and run the application. That are all the steps for publishing a WebService in plain old Java application.

 

 

 

 

And then answers comes back in, internally handled by the ca

5 Comments

  1. peter November 28, 2011
  2. Graham Williamson June 29, 2009
  3. Lucas Jellema June 29, 2009
  4. Graham Williamson June 24, 2009
  5. Emiel Paasschens June 8, 2009