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/autodeployBy 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
- 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.