JAVA API FOR XML WEB SERVICES (JAX-WS)
INTRODUCTION AND PURPOSE The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services. JAX-WS 2.0 replaced the JAX-RPC API in Java Platform, Enterprise Edition 5. The name change reflected the move away from RPC-style and toward document-style web services. It is part of the Java EE platform from Sun Microsystems. Like the other Java EE APIs. JAX-WS uses annotations, to simplify the development and deployment of web service clients and endpoints. The Reference Implementation of JAX-WS is developed as an open source project and is part of project GlassFish, an open source Java EE application server. It is called JAX-WS RI (For Reference Implementation). This Reference Implementation is now part of the Metro distribution (a web service stack). JAX-WS also is one of the foundations of WSIT.
USED WHEN AND WHERE JAX-WS is a technology for building web services and clients that communicate using XML. In JAX-WS, a remote procedure call is represented by an XML-based protocol such as SOAP. The SOAP specification defines the envelope structure, encoding rules, and conventions for representing remote procedure calls and responses. These calls and responses are transmitted as SOAP messages (XML files) over HTTP
USED WHEN AND WHERE On the server side, the developer specifies the remote procedures by defining methods in an interface written in the Java programming language. The developer also codes one or more classes that implement those methods. Client programs are also easy to code. A client creates a proxy (a local object representing the service) and then simply invokes methods on the proxy.
BASIC STEPS FOR CREATING THE WEB SERVICE AND CLIENT 1. Code the implementation class using JAX-WS Annotations. 2. Compile the implementation class. 3. Use wsgen to generate the artifacts required to deploy the service. 4. Package the files into a WAR file. 5. Deploy the WAR file. The tie classes (which are used to communicate with clients) are generated by the Application Server during deployment. 6. Code the client class. 7. Use wsimport to generate and compile the stub files. 8. Compile the client class. 9. Run the client
WRITING A WEB SERVICE package loanservice; import javax.jws.webservice; import javax.jws.webmethod; import javax.xml.ws.endpoint; @WebService public class LoanApprover { @WebMethod public boolean approve(string name) { return name.equals("mike"); }
WRITING A WEB SERVICE (CONTINUED) public static void main(string[] args){ LoanApprover la = new LoanApprover(); } } Endpoint endpoint = Endpoint.publish( "http://localhost:8080/loanapprover", la);
COMPILE THE SERVICE Create a myservice directory. From the directory just above loanservice, run Java s Annotation Processing Tool (APT): C:\>apt -d myservice loanservice/loanapprover.java This populates a directory named myservice. The directory holds the compiled package as well as a new directory (package) called jaxws. The new jaxws package holds classes associated with the parameters to and from each web service method. Use the -s switch
PUBLISH THE SERVICE From a directory just above myservice: C:\>java -cp myservice loanservice/loanapprover To view the WSDL, visit the service with a browser at http://localhost:8080/loanapprover?wsdl
GENERATE STUB CODE Make a client directory. C:\>wsimport p client keep http://localhost:8080/loanapprover?wsdl This populates the client subdirectory with.class and.java files.
WRITE THE CLIENT package client; class ApproverClient { public static void main(string args[]){ LoanApproverService service = new LoanApproverService(); LoanApprover approverproxy = service.getloanapproverport(); boolean result = approverproxy.approve("mike"); } } if(result) System.out.println("Approved"); else System.out.println("Not approved");
COMPILE & RUN THE CLIENT C:\>javac cp. client/approverclient.java C:\>java -cp. client/approverclient Approved
HOW IT WORKS
JAX-WS ANNOTATIONS Annotations play a critical role in JAX-WS 2.0 1. Annotations are used in mapping Java to WSDL and schema 2. Annotations are used in runtime to control how the JAX-WS runtime processes and responds to web service invocations Annotations utilized by JAX-WS 2.0 are defined in separate JSRs: JSR 181: Web Services Metadata for the JavaTM Platform JSR 222: JavaTM Architecture for XML Binding (JAXB) 2.0 JSR 224: JavaTM API for XML Web Services (JAX-WS) 2.0 JSR 250: Common Annotations for the JavaTM Platform
MORE ANNOTATIONS Web Services Metadata Annotations @WebService @WebMethod @OneWay @WebParam @WebResult JAX-WS Annotations @RequestWrapper @ResponseWrapper @WebEndpoint @WebFault @WebServiceClient @WebServiceRef
KEY FEATURES Uses SOAP Messages (also REST Msg Protocol) Over HTTP (also SMTP) Abstracts SOAP Messages from Developer Doesn t Have to Write Code to Parse Messages Supports RPC or Document Messaging Styles RPC - Simple Types i.e. xsd:long, xsd: byte, etc Document - Complex Types by Creating new XML Schemas Supports Asynchronous Requests via Thread Pools Faults can be Thrown back to Clients Can Insert Binary Data as the Payload or as a SOAP Attachment
KEY FEATURES Faults can be Thrown back to Clients Can Insert Binary Data as the Payload or as a SOAP Attachment New API s: JAX-WS, SAAJ, Web Service metadata New packages: javax.xml.ws, javax.xml.soap,javax.jws
ADVANTAGES / DISADVANTAGES Advantages: Easy to Create Simple Interoperable Services Protocol Hidden from Developer (but Available) Java Threading allows Asynchronous Services Faults (exceptions) can be Propagated Back to Client Disadvantages: Rapidly Changing s/w Versions / Tools Complex to Modify SOAP messages using API Binary Attachments Require Modifying WSDL
ROLE IN WEB SERVICE INFRASTRUCTURE JAX-WS is one of the core services in Web service Infrastructure. JAX-WS uses JAX-B to define binding of XML to Java artifacts. JAX-WS built on top of Axis2 Web services as an additional programming model. JAX-WS is designed to run on Java 5. Uses SOAP Messages (also REST Msg Protocol) Over HTTP (also SMTP)
REFERENCES JAX-WS Annotations: https://jax-ws.dev.java.net/jax-ws-ea3/docs/annotations.html JAX-WS Specification (JSR 224) http://jcp.org/en/jsr/detail?id=224 P r e s e n t a t i o n a b o u t J A X - W S http://gceclub.sun.com.cn/java_one_online/2006/ts-1194/ts-1194.pdf Java Web Services: Up and Running, by Martin Kalin. Copyright 2009 Martin Kalin, 978-0-596-52112-7 Sun Microsystems. The Java Web Services Tutorial for Java Web Services Developer s Pack, v2.0 February 17, http://java.sun.com/webservices/tutorial.html Core Java Volume 2, Advanced Features, Cay S. Horstmann and Gary Cornell, Sun Microsystems Press, Prentice Hall, Copyright 2008, 978-0132354790, pgs 871-882. Create JAX-WS Service in 5 Minutes (Tutorial), Alexander Ananiev & MyArch, http://myarch.com/create-jax-ws-service-in-5-minutes, Copyright 2002-2006 JAX-WS Tutorial http://www.javacoda.com/blog/?p=22 s t a c k o v e r f l o w Q & A s i t e. http://stackoverflow.com/questions/100993/rmi-vs-web-services-whats-best-forjava2java-remoting
CODING EXAMPLES How to modify JAX-WS Soap messages: http://blog.jdevelop.eu/2008/03/08/how-to-modify-jax-wssoap-messages/ JAX-WS Guide: http://axis.apache.org/axis2/java/core/docs/jaxws-guide.html Create a Web Service Client with JAX-WS: http://www.javadb.com/create-a-web-service-client-withjax-ws Explain JAX-WS technology for building web services and client that communicate using XML: http://java.boot.by/scdjws5-guide/ch04.html Creating a Simple Web Service and Client with JAX-WS: http://download.oracle.com/javaee/5/tutorial/doc/ bnayn.html