website stat

Web Services in Java 6

.NET 2.0 has a really easy way to create Web Services. Just attach annotations to the class and to the methods and all the stuff including stubs, WSDL files and SOAP Envelopes are generated on the fly.

Now Java, on its 6.0 version, has it too. Take a look at how easy it is to create a Web Service nowadays.

JAVA:
  1. package example;
  2. import javax.jws.WebService;
  3. import javax.jws.WebService;
  4. import javax.xml.ws.Endpoint;
  5.  
  6. @WebService public class Hello {
  7.  
  8. @WebMethod
  9. public String get(String hello) {
  10. return hello + " World";
  11. }
  12.  
  13. public static void main(String[] args){
  14. Hello hello = new Hello();
  15. Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello;
  16. }
  17. }

Now compile it and generate and the stuff

CODE:
  1. apt -dexample example/Hello.java
  2. java -cp example example.Hello

Now go to your web browser and point it to http://localhost:8080/hello?wsdl and there you go, your Web Service WSDL ready to be used. On the other end, ie, the client, you just need to import it and you'll an object readily available for manipulation.

CODE:
  1. wsimport -p client -keep http://localhost:8080/hello?wsdl

Now you can use your Web Service directly on your Java application through the HelloService class.

Have Fun!

P.S. - In case you're wondering how can it simply work without having a web server container like Tomcat or Jetty, that's because Java 6 has a light-weight web server built in.


2 Responses to “Web Services in Java 6”

  1. Sérgio Carvalho
    Published at March 28th, 2007 at 8:37 pm

    SOAP is, bluntly, a stinking piece of crap. Before praising soap, try creating a moderately complex interface and then access it from three different implementations. i.e. export in Java, use in .net, python, perl, php, gSoap(c/c++).

    A remote call interface that only works within the same implementation is worthless.

  2. mlopes
    Published at March 28th, 2007 at 9:13 pm

    Who talked about SOAP? With some small changes you can easily implement a RESTful interface with that code!

    I did not praise SOAP anywhere in my post but I’ve actually coded a seamlessly coupled application with Java and .NET and worked great. Yes, there are small changes in the WSDL and in the SOAP Envelopes but you just have to be careful.

    Nevertheless, I prefer REST a lot more.