1 / 21

Robert Thornton

Web Services with Apache CXF Part 2 : JAXB and WSDL to Java. Robert Thornton. Notes. This is a training, NOT a presentation Please ask questions This is being recorded https://tech.lds.org/wiki/Java_Stack_Training Prerequisites Maven Spring Web Application Development

geneva
Télécharger la présentation

Robert Thornton

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton

  2. Notes • This is a training, NOT a presentation • Please ask questions • This is being recorded • https://tech.lds.org/wiki/Java_Stack_Training • Prerequisites • Maven • Spring • Web Application Development • Web Services, Part I: SOAP

  3. Objectives At the end of this presentation, the participant will be able to: • Understand the role of JAXB as a web service data binding solution. • Model data entities using JAXB annotations. • Understand the purpose and usage of the CXF WSDL2Java tool. • Be able to use WSDL2Java to generate a client proxy in a stand-alone Java application. • Be able to configure Spring to manage a generated WSDL2Java client proxy . • Be able to use JAXB as the data binding solution in a Java Stack application.

  4. Java and XML The Marriage of XML and Java: • XML is a data markup language. • Used for long or short-term data storage. • Useful for data transfer between vastly different architectures. • Particularly useful for web service architectures. • Java is an object-oriented programming language. • Unmarshalls (reads) data from existing XML into Java data objects. • Performs manipulations on Java objects via services. • Marshalls (writes) Java objects into a new XML representation.

  5. Java and XML: Choices, choices…. The marriage of Java and XML has produced many child technogies, strategies, and libraries: • DOM • SAX • JAXP • DOM4J • JAXB • XML Beans • JDOM • XStream • and many more….

  6. Java and XML: Overview Most Java-XML strategies fall into three camps: • DOM (Document Object Model) • Used to represent the entire document model in memory as hierarchical nodes in the document tree. • XML-to-Object Binding • Used to represent XML types and elements as Java types and objects and vice-versa. • SAX (Simple API for XML) • An event-based API for operating on each piece of the XML document individually and in sequence. Most often used to parse DOM trees or construct XML Object bindings. In practice, most solutions use some combination of these.

  7. JAXB: A Data Binding Solution The JAXB API is the standard solution provided by the JDK for Java XML data binding: • Java classes are bound to XML types, elements, and attributes through Java annotations. • A SAX event-based parser is used to parse XML documents and construct Java objects as well as to write Java objects back to XML. • The XJC tool (included in the JDK) can convert an existing XML schema into Java classes or create an XML schema from annotated Java classes.

  8. JAXB and Web Services As a data modeling API, JAXB is particularly useful to web services: • XML is the most common form of data transport. • Annotated Java classes can be made to represent XML schema types. • JAXB APIs can unmarshall XML into Java objects and back again. * Note that the CXF web service framework automatically handles the marshalling and unmarshalling of XML data to and from JAXB annotated Java classes.

  9. JAXB: Marshalling and Unmarshalling Although CXF typically handles the marshalling and unmarshalling for you, it can be helpful to know how to do it “manually”. For example: • It allows you to experiment with how JAXB annotations affect the parsing and rendering of XML. • It helps you debug issues that arise from data being marshalled or unmarshalled incorrectly.

  10. JAXB: Unmarshalling JAXB makes unmarshalling from XML easy: // Just create a JAXB context for your Java data classes JAXBContext jaxb = JAXBContext.newInstance(myClasses); // Then unmarshall the XML into instances of those classes. MyClass obj = jaxb.createUnmarshaller().unmarshall(xml) The Unmarshaller can accept XML input as a character stream, a file, a DOM node, or a number of other input types.

  11. JAXB: Marshalling Marshalling back into XML from Java object is just as easy: // Create a JAXB context for your Java data classes JAXBContext jaxb = JAXBContext.newInstance(myClasses); // Marshall the Java objects their XML representation. jaxb.createMarshaller().marshall(myObject, output); The Marshaller can serialize the XML to a character stream, a file, a DOM node, or several other output types.

  12. JAXB: The Context Instances of the JAXBContext class effectively represent an “in-memory” schema of your data: • It’s a registry of all the classes that can be bound to XML types. • It’s a factory for Marshaller and Unmarshaller instances. • It can be supplied listeners and a Schema object for additional or custom validation.

  13. JAXB: Annotations Although JAXB can bind almost any data object with little or no annotations, annotations are typically desirable, for example: • They can tell JAXB whether to unmarshal a field into an attribute or an element. • They can inform JAXB of ID fields, element order, and other schema constraints. • They can be used to identify or customize schema types, element names, attribute names, element wrapping, etc.

  14. JAXB: Common Annotations JAXB defines many annotations to customize the Java-XML data binding. Here are just a few: • @XmlRootElement • @XmlType • @XmlElement • @XmlAttribute • @XmlID • @XmlElementRef • @XmlElementWrapper • @XmlTransient These and more can be found in the following package: • javax.xml.bind.annotation

  15. Some general rules about JAXB annotations: • JAXB cannot bind Java interfaces to XML types. • Your class can reference an interface on a field if you annotate it with an @XmlElementRef that identifies the concrete type. • Apply JAXB annotations to either abstract or concrete types. • Annotations on Java class properties must be placed on the fields or the setters but not both.

  16. Apache CXF: SOAP: Lab 1 Lab 1: JAXB Data Binding http://tech.lds.org/wiki/Web_Services_with_Apache_CXF_-_Part_2

  17. Consuming 3rd Party Web Services Third-party SOAP web services are typically consumed in one of the following ways: • Using a client JAR that contains the necessary Java classes and service proxies to access the web service. • Using a WSDL-to-Java tool to automatically generate a web service proxy from a published WSDL.

  18. WSDL to Java CXF provides the wsdl2java tool to consume third-party SOAP services: • wsdl2Java

  19. Apache CXF: SOAP: Lab 2 Lab 2: Using WSDL 2 Java http://tech.lds.org/wiki/Web_Services_with_Apache_CXF_-_Part_2

  20. Apache CXF: SOAP: Lab 3 Lab 3: WSDL 2 Java in the Java Stack http://tech.lds.org/wiki/Web_Services_with_Apache_CXF_-_Part_2

  21. Resources On the web: • http://cxf.apache.org • http://www.w3.org/TR/soap/ • http://en.wikipedia.org/wiki/Cxf • http://en.wikipedia.org/wiki/SOAP • http://ajaxonomy.com/2008/xml/web-services-part-1-soap-vs-rest In Print: • Developing Web Services with Apache CXF and Axis 2, Kent Kai Iok Tong, TipTech Development, 2005-2010. ISBN: 978-0-557-25432-3

More Related