Web Services and vi !

Web Services and vi

Yesterday, I spoke at JavaOne, presenting the Sun Java System Application Server Platform Edition 9.0 to the general public. People were excited to hear about the new features but most importantly people loved to hear that we were now a commercially supported open source project. The project Glassfish is as far as I know one of the few commercially supported open source project. I think this is an exciting development to see the code and the expertise shared. We released the source code so people can fix bugs, provide features or just participate to discussions. And this is all free, including the docs, unlike JBOSS who charges for docs for instance. But what I think really differentiate us is the fact we have a support infrastructure in place which can be used when applications go into production, we have great field engineers and market development engineers who are ready to help with customers, on top of what all open source projects do, mailing lists and forums.

Now let's dive into the real issues that brought me here today. How can you develop a WebService in 5 minutes chrono with vi (or notepad...). I did this demo yesterday at JavaOne and for those of you who missed it, I am now bringing it to you...

Web Service implementation

First you need a editor, I used vi, take the one you like. The simplest web service you can think is of course the "hello world" example. This is the complete code below, just a few line of code and that's all you need.

package endpoint;

import javax.jws.WebService;

@WebService
public class Hello {

    public String sayHello(String param) {
        return "Hello " + param;
    }
}

now to deploy this powerful web service in glassfish, you just need to do

javac -cp $GLASSFIH_HOME/lib/jaxrpc-impl.java:$GLASSFISH_HOME/lib/j2ee.jar:. -d $GLASSFISH_HOME/domains/domain/autodeploy

By compiling directly into the autodeploy directory (-d option), you have in fact deployed this web service. you should ensure that the deployment went fine by controlling the log file ($GLASSFISH_HOME/domains/domains/logs/server.log).

You can browse the wsdl by accessing the following URL [http://localhost:8080/Hello/HelloService?WSDL Be aware that the URL could be different in your environment, again study the log file mentioned above, it tells you the URL for the deployed endpoint in one of the messages that popped up during deployment.

That's it ! You wrote your first jax-ws 2.0 web service and deployed, on to testing it...

Web Service Client

The client side of things is a bit more complicated... First you need to remember that with Web Services, the WSDL is the interface against which you can program your clients. There is not such a thing like in EJB where you have a java interface. Since Web Services is about interoperablity, WSDL is the reference and the description of your endpoint capabilities. So what you need to do to be able to program your client access code at ease is generate a java interface from the WSDL (that's right, the exact one that was published when you deployed your web service earlier). You could use the wsimport tool and study the resulting artifacts but since I have little customized my @WebService annotation, I can make from the jax-ws docs a few educated guesses :

  • the package name for the client classes will be the same as the server with "jaxws" appended (in our case, it becomes endpoint.jaxws)
  • the Service interface is the web service implementation simple class name + "Service"
  • the port is the web service implementation simple class name
So below is the code for the simple client example, the @WebServiceRef will be used to point to the WSDL file defining the web service. The service static variable will be injected by the application client container and the rest of the code is pretty much straightforward.

Now do the following :
  • wsimport http://localhost:8080/Hello/HelloService?WSDL
    this will create the client artifacts
  • compile the class below (remember to resuse the same classpath).
  • run the application client container : appclient client.Client

And you should see the result of the invocations !

package client;

import javax.jws.WebServiceRef;

import endpoint.jaxws.HelloService;
import endpoint.jaxws.Hello;

public class Client {

        @WebServiceRef
        static HelloService service;

        public static void main(String[] args) {

            try {
                Hello port = service.getHello();
                for (int i=0;i<10;i++) {
                    System.out.println(port.sayHello("blog readers !"));
                }

            } catch(Exception e) {
                e.printStackTrace();
            }
       }
}

Conclusion

That was easy ! few things to remember :
  • I basically just compiled and run both the web service server and clients. We now have the same developer experience in Java EE 5 and with Java SE. No more packaging, no more stubs, no real deployment either...
  • Everything is ready on the glassfish website to get you started. The implementation is not finished of course but you can start today and give us our feedback, you can help in many ways.
, ,
Comments:

Some of thing I described in this blog have changed since then. 1. The javax.jws.ServiceRef is now javax.xml.ws.WebServiceRef 2. The client classes generated by the wsimport do not have jaxws appended anymore so they also live in the endpoint package. Therefore the client must be modified accordingly : import endpoint.jaxws.HelloService; import endpoint.jaxws.Hello; should now be import endpoint.HelloService; import endpoint.Hello;

Posted by Jerome Dochez on September 26, 2005 at 06:15 AM PDT #

Hello, I've just a - stupid - question: I'm currently using Sun Java™ System Application Server Plateform Edition 9.0. I saw you were a Glassfish addict. Can you tell me what is the difference between this two plateforms? Thanks.

Posted by Jean-Marie on May 09, 2006 at 11:07 PM PDT #

This is what I find after one week googling internet I finaly find it via yahoo search, thank you for your simple tutorial, all the other are based on ant and deployment steps aren't understanadable

Posted by Reza Ravasizadeh on September 10, 2006 at 03:44 AM PDT #

Thanks for the information it was a good read.

Posted by SEO company on July 10, 2009 at 05:41 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

dochez

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today