The Source for Java Technology Collaboration
User: Password:



Vivek Pandey

Vivek Pandey's Blog

Webservices in JDK 6

Posted by vivekp on December 12, 2006 at 05:33 PM | Comments (24)

Java SE 6 is out! Among other things it has exciting new end-to-end web services stack  - JAXWS 2.0 FCS RI. Couple of main things you should know:
  • JAXWS Tools wsimport and wsgen part of JDK
  • Simplified deployment using Endpoint API and light-weight HTTP Server in JDK
  • Uses JAXB 2.0, also part of JDK6, for all data binding needs.
  • Also uses Stax, SAAJ 1.3 for message processing. Both these jars are part of JDK 6.

Web service Endpoint

Lets start with a POJO annotated with @WebService annotation. This annotation tells JAXWS that its a Web Service endpoint. You may like to annotate the methods that will be exposed as web services method with @WebMethod annotation. You dont need to specify it if all the methods will be exposed as web services method.

Calculator.java

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

@WebService
public class Calculator {
    @WebMethod
    public int add(int a, int b) {
        return a+b;
    }
    
    public static void main(String[] args){
        // create and publish an endpoint
        Calculator calculator = new Calculator();
        Endpoint endpoint = Endpoint.publish("http://localhost:8080/calculator", calculator);        
    }
}

Now you need to do 2 things to build and publish this endpoint:

  • Run apt to compile and generate required wrapper classes
    • apt -d sample example/Calculator.java
  • Publish
    • java -cp sample example.Calculator

Thats it! You have deployed your web services endpoint. Now, try accessing the deployed WSDL by typing http://localhost:8080/calculator?wsdl in your web browser to see the published WSDL. No need to provide deployment descriptor, starting a container etc. Its all done for you internally by JAXWS using light-weight HTTP server available in JDK 6. In short - extremely fast prototyping!

Web Services Client

Lets see how we develop a client based on proxy.

Run wsimport

You would run wsimport on the deployed WSDL URL:

wsimport -p client -keep http://localhost:8080/calculator?wsdl

This step will generates and compile some classes. Notice -keep switch, you need it to keep the generated Java source files. By default wsimport only leaves behind the compiled class files. In this example the classes that matters are

  • Calculator.java - Service Endpoint Interface or SEI
  • CalculatorService - Generated Service, instantiate it to get the proxy

Invoke the endpoint

CalculatorApp.java

package client;
class CalculatorApp {
	public static void main(String args[]){
        /**
         * Instantiate the generated Service
         */ 
    	CalculatorService service = new CalculatorService();
        
        /**
         * Get the port using port getter method generated in CaculatorService
         */
        Calculator calculatorProxy = service.getCalculatorPort();
        
        /**
         * Invoke the remote method
         */
        int result = calculatorProxy.add(10, 20);
        System.out.println("Sum of 10+20 = "+result);
	}
}

Now that you have your client code is ready simply compile it:

javac -cp . CalculatorApp.java

and run:

java -cp . client.CalculatorApp

It will print:

Sum of 10+20 = 30

Running latest JAXWS RI on JDK6

The obvious question might be that how would you use latest JAXWS 2.1 RI on top of JDK6. JAXWS 2.1 RI is feature complete and we are busy fixing bug. For list of JAXWS 2.1 features and plan refer to the JAXWS 2.1 roadmap.

All you need to do is to use Endorsed Directory Mechanism  to point to the lib directory in JAXWS intallation:

Runtime

  • java -cp . -Djava.endorsed.dirs=$JAXWS_HOME/lib client.CalculatorApp

Tools

  • Run the tools from JAXWS distribution
    • export JAXWS_HOME to your JAXWS distribution installation
    • $JAXWS_HOME/bin/wsimport
    • $JAXWS_HOME/bin/wsgen
  • For wsimport set WSIMPORT_OPTS="-Djava.endorsed.dirs=$JAXWS_HOME/lib"
  • For wsgen set WSGEN_OPTS="-Djava.endorsed.dirs=$JAXWS_HOME/lib"

Use JDK 6 to develop your web services application and continue providing feedback to users@jax-ws.dev.java.net and if you find an issue report them at IssueTracker.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Does JDK 6.0 also support web services security 1.1? or we have to download JWSDP 2.x or WSIT separately? Thanks.

    Posted by: rayymlai on December 12, 2006 at 11:55 PM

  • Does apt internally invoke wsgen code?

    Posted by: ss141213 on December 13, 2006 at 08:07 AM

  • There were a couple of recent blog entries about using JAX-WS 2 with the latest NetBeans. It's even easier and you only need JDK 5.

    Can't find those entries but here's an article from Sun site.

    Posted by: madth3 on December 13, 2006 at 09:41 AM

  • rayymlai,

    No, I dont think WSS is part of JDK 6.0. JWSDP is EOLed. You should try WSIT. Try GF v2 or get it from http://wsit.dev.java.net.

    -vivek.

    Posted by: vivekp on December 13, 2006 at 03:27 PM

  • Its otherway, wsgen internally invokes apt. You can run wsge directly, but you will need to compile it first.

    Posted by: vivekp on December 13, 2006 at 03:29 PM

  • Great article.
    The second instance of "import javax.jws.WebService;" should be "import javax.jws.WebMethod;"


    Posted by: wsurowiec on December 13, 2006 at 08:17 PM

  • A possible clarification for others trying this too late at night (almost midnight my local time): manually add (copy and paste) the code for client/CalculatorApp.java and then javac -cp . client/CalculatorApp.java

    Posted by: wsurowiec on December 13, 2006 at 08:25 PM

  • I think the best part about being able to deploy web services without a container is how helpful that is for creating unit tests. I would love to see netbeans make testing web services a snap by taking advantage of this feature.

    Posted by: lucasjordan on December 14, 2006 at 05:49 AM

  • he doc for the "apt" tools says that support for "apt" may be discontinued in future JDK releases. (http://java.sun.com/javase/6/docs/technotes/tools/share/apt.html ). Given this, is "wsimport" the right tool to generate the "artifacts" followed by "javac". ?. /rk

    Posted by: rktumuluri on December 26, 2006 at 04:30 AM

  • Is there anyway to turn on "logging" for the http server that is built-in into the JDK ?. I am referring to the http server used in the examples for web-services in the JDK.

    /rk

    Posted by: rktumuluri on December 26, 2006 at 04:31 AM

  • Nice short example. Thanks - Gary

    Posted by: garyfreder on December 26, 2006 at 02:19 PM

  • It seems to me that the ant tasks provided by the JAX WS project are missing in jdk1.6.0 (at least I can't find wsgen anttask in tools.jar)... Did I miss something?
    -Olivier

    Posted by: oliv on January 15, 2007 at 02:53 PM

  • To Oliver: Only wsimport command line tool is included in JDK6. For wsimprot ant task youhave to get it from standalone JAXWS 2.0 or 2.1 RI.

    Posted by: vivekp on January 18, 2007 at 10:56 AM

  • I think the best part about being able to deploy web services without a container is how helpful that is for creating unit tests. I would love to see netbeans make testing web services a snap by taking advantage of this feature.

    Posted by: alicia67 on February 12, 2007 at 12:26 AM

  • The link provided for "Endorsed Directory Mechanism" isn't valid; the new one should be http://java.sun.com/javase/6/docs/technotes/guides/standards/

    Posted by: ivanvenuti on March 03, 2007 at 01:11 AM

  • I am using Linux. When I ran the apt command, I encountered this:


    schandran@5[code]$ apt -d out Calculator.java
    warning: Annotation types without processors: [javax.xml.bind.annotation.XmlRootElement, javax.xml.bind.annotation.XmlAccessorType, javax.xml.bind.annotation.XmlType, javax.xml.bind.annotation.XmlElement]
    error: Could not get TypeDeclaration for: example.Calculator in apt round: 2
    Problem encountered during annotation processing;
    see stacktrace below for more information.
    java.lang.NullPointerException
    at com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceAP.completeModel(WebServiceAP.java:382)
    at com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceAP.process(WebServiceAP.java:238)
    at com.sun.mirror.apt.AnnotationProcessors$CompositeAnnotationProcessor.process(AnnotationProcessors.java:60)
    at com.sun.tools.apt.comp.Apt.main(Apt.java:454)
    at com.sun.tools.apt.main.JavaCompiler.compile(JavaCompiler.java:258)
    at com.sun.tools.apt.main.Main.compile(Main.java:1102)
    at com.sun.tools.apt.main.Main.compile(Main.java:964)
    at com.sun.tools.apt.Main.processing(Main.java:95)
    at com.sun.tools.apt.Main.process(Main.java:43)
    at com.sun.tools.apt.Main.main(Main.java:34)


    I directly did javac and executed the main() method, the webservice got published!

    Posted by: subwiz on March 23, 2007 at 02:07 AM

  • Thanks, Vivek! This is great!
    I had no idea how easy it was to publish actual, working WS Endpoints so quickly.
    I think everyone will be using SE 1.6 pretty soon.
    --Bob

    Posted by: consman on November 18, 2007 at 08:56 PM

  • Excellent an example. Any example how to publish web services using JDK Lightweighted HttpServer and HttpsServer API?

    Posted by: ywang880 on November 19, 2007 at 06:12 PM

  • Hi Vivek,


    I am trying with the example you have given.

    While executing the command 'apt -d sample example/Calculator.java' ; I am receiving the error

    warning: Annotation types without processors: [WebMethod]
    1 warning
    warning: Annotation types without processors: [javax.xml.bind.annotation.XmlRoot
    Element, javax.xml.bind.annotation.XmlAccessorType, javax.xml.bind.annotation.Xm
    lType, javax.xml.bind.annotation.XmlElement]
    1 warning
    Calculator.java:10: cannot find symbol
    symbol : class WebMethod
    location: class example.Calculator
    @WebMethod
    ^
    1 error


    Can anybody help me in thsi regard.


    Thanks
    Rishi
    rishirraghav@yahoo.com

    Posted by: rraghavus on February 29, 2008 at 02:23 AM

  • Mine is telecom domain, so I need create a web service where it do some stuff (allocate/de allocate bandwidth or service creation etc) and send a response as Object. SO how do I create a web service which gives a object as result. I am also intrested to see some methods of that object.

    Posted by: seetaram on March 12, 2008 at 11:17 PM

  • Hi Rishi,
    This can best be answered at Metro forum, can you post your question to forum and provide a reproducible testcase?

    Posted by: vivekp on March 13, 2008 at 09:46 AM

  • Hi seetaram,
    You should post your question to Metro forum as we would like to understand it better as what you are trying to do specially with allocate/de-allocate bandwidth etc. For returning an Object as result there are more than one ways to do it, one is by defining schema type, such as wild card that would map to an Object or using Provider mechanism and return either Source or a JAXB Object. Metro with GlassFish is used by large telecom companies in their solutions so I am sure your requirements would definitely be met by Metro.

    Posted by: vivekp on March 13, 2008 at 09:52 AM

  • Sounds good Vinay.
    Thanks for your reply. I try to do. I ask queries.. if i have doubts.

    Posted by: seetaram on May 31, 2008 at 01:04 AM

  • Vivek, can you please provide me link for ws stuff in java.

    Posted by: seetaram on May 31, 2008 at 01:52 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds