Java Web Services [4/5]: Java API for XML Web Services

  • Published on

  • View

  • Download


Presentations for Java Web Services Course, September 2010


  • 1. Topic 4Java API for XML Web ServicesAssoc.Prof. Dr. Thanachart Numnonda www.imcinstitute.comAugust 2010

2. Agenda Java APIs for Web Services JAX-RPC JAX-WS 2 3. Java APIs for Web Services 3 4. JWSDP Java Web Services Development Pack (JWSDP) is a freesoftware development kit (SDK) for developing WebServices JWSDP has been replaced by GlassFish and WSIT andseveral components are in Java SE 6. JWSDP APIs Java API for XML Processing (JAXP), v 1.3 Java Architecture for XML Binding (JAXB), v 1.0 and 2.0 JAX-RPC v 1.1 JAX-WS v 2.0 SAAJ (SOAP with Attachments API for Java) Java API for XML Registries (JAXR) 4 5. Java API for XML Processing (JAXP) JAXP enables applications to parse, validate andtransform XML documents. Java developers can invoke SAX parser DOM parser The main JAXP APIs are defined in thejavax.xml.parsers package SAXParserFactory DocumentBuilderFactory 5 6. Java API for XML Binding (JAXB) JAXB allows Java developers to access and process XML datawithout having to know XML or XML processing. JAXB is a Java technology that enables you to generate Javaclasses from XML schemas by means of a JAXB bindingcompiler. unmarshalling XML instance documents into Java content trees marshalling Java content trees back into XML instance documents.6 7. SAAJ (SOAP with Attachments API for Java) SAAJ provides a convenient API for constructingand sending SOAP messages without having todirectly create the XML yourself. SAAJ was originally part of the Java APIfor XML Messaging (JAXM) Separated out from JAXM 1.0 into JAXM 1.1 and SAAJ 1.1 (SAAJ 1.3 (Java EE 5))7 8. JAXR Standard Java API for performing registry operationsover diverse set of registries Web service publication & discovery A unified information model for describing businessregistry content Provides multi-layered API abstractions Level 0: for UDDI Level 1: for ebXML registry/repository 8 9. JAX-RPC XML data types to/from Java types mapping SOAP Message Handler framework WSDL to/from Java mapping Servlet-based Web service endpoint model JAX-RPC Client Programming Models Extensible type mapping9 10. JAX-WS (JAX-RPC 2.0) New handler framework Metadata-based Web services Operational style improvement Asynchronous Web services Support for non-HTTP transport Interoperable attachments (as part of WS-I) Secure Web services (as part of Security)10 11. JAXM JAXM is not part of Java EE 5 JAXM-based asynchronous messaging on Java EE 5is supported via Message-driven Bean (MDB) in EJB 3 can receiveboth JMS and JAXM message types Connector 1.5 allows pluggability of various typesmessage providers (JAXM provider and JMSprovider)11 12. Java APIs for Web Services Protocols SOAP JAXM 1.0 (JSR 67), SAAJ JAX-RPC => JAX-WS 2.0 (JSR 224) WSDL JAX-RPC => JAX-WS 2.0 (JSR 224) UDDI JAXR 1.0 (JSR 93)12 13. Suns Web Services Stack 13 14. JAX-RPC14 15. Remote Procedure Call (RPC) RequestResponseClient SystemServer System RPC, COM, CORBA, RMI:Synchronous communication: calling process blocks untilthere is a responseMore tightly coupled (than non-RPC model): client mustfind recipients and know method and its argumentsNon persistent 16. Remote Procedure CallsClient InterfaceInterface Servant Stub Skeleton Run TimeProtocol Run Time Common Interface between client and Server Stub for client, Skeleton/Tie for server On-the-wire protocol needs to be agreed upon 17. RPC Example Java RMI Java Remote Interface rmic compilerClient Remote Object ImplementationRMI Client RMI ServerStubSkeleton Java Remote Method Protocol (JRMP) is On-the-wire protocol 18. RPC Example - JAX-RPC 19. Common Interface Service is described in IDL (Interface DescriptionLanguage) IDL ofCORBA service Java RMI interface in RMI (Language specific) WSDL for Web service Used by tools to statically or dynamically generateand/or configure interfaces, proxies, and ties in anetwork technology specific environment 20. What is JAX-RPC? Java API for XML-based RPC Web Services operations are performed by exchangingSOAP 1.1 messages Services are described using WSDL WSDL is the contract between service provider andclient (only contract needed) Web service endpoints and clients use JAX-RPCprogramming model Key technology for Web Services in the J2EE 1.4platform 21. JAX-RPC Design Goals Easy to use programming model For both defining & using a service Hides all the plumbing You dont have to create SOAP messages yourself SOAP and WSDL-based interoperability Interoperate with any SOAP 1.1 compliant peers Extensibility and Modularity Support new versions of XML specification, i.e. SOAP 1.2and beyond Message handler architecture 22. JAX-RPC ArchitectureJAX-RPC JAX-RPCClientWSDL DocumentService Endpoint GeneratedJavaWSDL WSDLJava CodeContainer ContainerClient-side JAX-RPC Server-side JAX-RPCRuntime SystemRuntime SystemSOAPHTTP 23. JAX-RPC Runtime System Core of a JAX-RPC implementation: Library that provides runtime servicesfor JAX-RPC mechanisms Implements some of the JAX-RPC APIs Client side: Can be implemented over J2SE, J2EE or J2MEplatforms Server-side: J2EE 1.3 or 1.4 Containers: EJB or Servlet 24. Developing a Web Service Interfaces (java.rmi.Remote type) Must follow JAX-RPC conventions Implementation classes Servlet-based endpoint model Optional handler and serializer classestool WSDLService contract Packaged application (war file) 25. JAX-RPC : Web Service EndpointJAX-RPC Service EndpointWSDLJavaWSDL Document Container Server-SideJAX-RPC Runtime System SOAP HTTP 26. JAX-WS 26 27. Quick overview of JAX-WS Easy way to use Java API for XML Web Services Replace JAX-RPC Just add @annotation to POJO XML Descriptor free programming Layer architecture SOAP 1.2 (Document/Literal) Use JAXB for data binding Protocol and transport independence Part of Java SE6 and Java EE 5 Platform27 28. JAX-WS Layered Architecture28 29. What Does It Mean? Upper layer uses annotations extensively Easy to use Great toolability Fewer generated classes Lower layer is more traditional API-based For advanced scenarios Most application will use the upper layer only Either way, portability is guaranteed 29 30. Two ways to create a Web Service Starting from a WSDL file> Generate classes using wsimport (=> xjc)> WS interface> WS implementation skeleton class> Implement WS interface> Build and deploy Starting from a Java class (POJO)> Annotate POJO> Build and deploy> WSDL file generated automatically 30 31. Start with a Java Class31 32. Server-Side Programming Model: POJO1Write a POJO implementing the service2Add @WebService annotation to it3Build and Deploy the applicationWSDL is automatically generated at runtime1Point your clients at the WSDLe.g. http://myserver/myapp/MyService?WSDL 32 33. Example 1: Servlet-Based Endpoint@WebServicepublic class Calculator {public int add( int a, int b) {return a + b;}} @WebService annotation All public methods become web service operations WSDL/Schema generated automatically Default values are used 33 34. Service Description Default Mapping Java mapping => WSDL 34 35. Example 2: EJB-Based Endpoint@WebService@Statelesspublic class Calculator {public int add( int a, int b) {return a + b;}} Its a regular EJB 3.0 component, so it can use anyEJB features > Transactions, security, interceptors...35 36. Customizing through Annotations@WebService(name=CreditRatingService,targetNamespace= class CreditRating { @WebMethod(operationName=getCreditScore) public Score getCredit( @WebParam(name=customer) Customer c) { // ... implementation code ...}}36 37. Start with a WSDL file 37 38. Generating an interface from WSDL WSDL => Java generation38 39. Implementing a Web Service from a Generated Interface39 40. Server Side Web Service40 41. JAX-WS uses JAXB for Data Binding41 42. add() Parameter : Example42 43. JAXB XML Schema to Java Mapping43 44. Client Side Programming44 45. Java SE Client Side Programming Point a tool (NetBeans or wsimport) at the WSDLfor the service wsimport annotated classes and interfacesCall new on the service classGet a proxy using a getPort methodInvoke any remote operations 45 46. Example : Java SE-Based ClientCalculatorService svc = new CalculatorService();Calculator proxy = svc.getCalculatorPort();int answer = proxy.add(35, 7); No need to use factories The code is fully portable XML is completely hidden from programmer 46 47. Java EE Client Side Programming Point a tool (NetBeans or wsimport) at the WSDLfor the servicewsimport Generate annotated classes and interfaces Inject a @WebServiceReference of theappropriate type No JNDI needed Invoke any remote operations47 48. Example : Java EE-Based Client@Statelesspublic class MyBean { // Resource injection @WebServiceRef(CalculatorService.class) Calculator proxy;public int mymethod() { return proxy.add(35, 7);} 48 49. Client Side49 50. Annotations Used in JAX-WS JSR 181: Web Services Metadata for theJava Platform JSR 222: Java Architecture for XMLBinding (JAXB) JSR 224: Java API for XML Web Services(JAX-WS) JSR 250: Common Annotations for theJava Platform 51. @WebService Marks a Java class as implementing aWeb Service, or a Java interface asdefining a Web Service interface. Attributes> endpointInterface> name> portName> serviceName> targetNamespace> wsdlLocation 52. @WebMethod Customizes a method that is exposed asa Web Service operation The method is not required to throwjava.rmi.RemoteException. Attributes > action > exclude > operationName 53. @WebParam Customizes the mapping of an individualparameter to a Web Service messagepart and XML element. Attributes> header> mode> name> partName> targetNamespace 54. @WebResult Customizes the mapping of the returnvalue to a WSDL part and XML element. Attributes > header > name > partName > targetNamespace 55. JAX-WS - Provider Web Service endpoints may choose to work atthe XML message level by implementing theProvider interface. The endpoint accesses the message ormessage payload using this low-level, genericAPI Implement one of the following> Provider> Provider> Provider 56. JAX-WS - Provider 57. JAX-WS - Dispatch Web service client applications may choose towork at the XML message level by using theDispatch APIs. The interfaceprovides support for the dynamic invocation ofservice endpoint operations. Similar to Provider on server side 58. JAX-WS - Dispatch 59. Resources Some contents are borrowed from the presentationslides of Sang Shin, Java Technology Evangelist,Sun Microsystems, Inc. Web Services and Java, Elsa Estevez, TomaszJanowski and Gabriel Oteniya, UNU-IIST, Macau59 60. Thank you www.imcinstitute.com60


View more >