The Java Architecture for XML Binding API (JAXB) makes it easy to access XML documents from applications written in the Java programming language. This document shows you how NetBeans IDE 6.1 and 6.0 provide tooling support for JAXB, principally by means of a wizard that turns various types of XML documents into Java classes. Starting with a given WSDL file, we will generate Java classes and then do something with them.
In this tutorial, you will learn two specific things:
For detailed information on the JAXB architecture, as well as on JAXB as a whole, see Chapter 2: Binding between XML Schema and Java Classes and Chapter 3: Using JAXB in The Java Web Services Tutorial.
Contents
To follow this tutorial, you need the following software and resources.
Both Tomcat and GlassFish can be installed with the Web and Java EE distribution of NetBeans IDE. Alternatively, you can visit the GlassFish downloads page or the Apache Tomcat downloads page.
The goal of this exercise is to create a project and generate Java objects from an XML document.
Creating a project in NetBeans 6.1 includes new options which can be left at the default. For example, the Use Dedicated Folder for Storing Libraries checkbox may be left unselected.
Right-click the JseSimpleClientReport node and choose New > Other > XML > JAXB Binding. Then click Next. The New JAXB Binding wizard appears.
The settings in the wizard above serve the following purposes:
Click Finish.The IDE generates the Java objects from the given XML document. In the next section, we examine the Java objects in the IDE.
The goal of this exercise is to familiarize ourselves with the tools in NetBeans IDE for working with the JAXB wizard's output.
The Projects window displays a new node that contains the WSDL file, as shown below. Note that you can right-click the CreditReport node and then the wizard reopens and you can change the settings you specified earlier.
Also note that the wizard has put the JAXB libraries on the application's classpath for JDK 5. For JDK 6, the JDK itself provides the JAXB libraries, so no new nodes are added to the Libraries node.
Assuming you have changed the settings in the wizard, you can regenerate the Java objects, as indicated below:
Right-click the WSDL file and choose Open. The document opens in an editor with three tabs, "Source", "WSDL", and "Partner". Click WSDL and notice that a visual view opens, allowing you to easily analyze and edit the WSDL file.
Now that you know what the IDE has generated for your XML document, we will use some of the tools we have looked at to do something meaningful with our generated Java objects.
The goal of this exercise is to do something meaningful with the files and code that the IDE has generated for you. You will set some values in one of the generated Java objects and then marshall it to the IDE's Output window.
public static void main(String[] args) {
CreditReport cr = new CreditReport();
}
Now just type 'cr.', to start using your declaration, and the IDE gives you relevant code completion for your JAXB artifacts (on some systems you may need to press Ctrl-Space):
Set some values for the JAXB class, such as the following (Add an import statement for java.math.BigInteger):
cr.setFirstName("Butros Butros");
cr.setLastName("Gali");
cr.setDob("1930/05/30");
cr.setScore("900");
cr.setSsn("123-45-6789");
cr.setLatestAddress1("2500 Some Ave");
cr.setLatestAddress2("Suite 5000");
cr.setCity("New York");
cr.setState("New York");
cr.setCountry("USA");
cr.setPostalCode("NY 12345-6789");
cr.setCurrency("USD");
cr.setLiability(BigInteger.valueOf(2000000));
cr.setLiquidAssests(BigInteger.valueOf(3000000));
cr.setImmovableAssests(BigInteger.valueOf(5000000));
Type the letters 'jaxbm' into the editor. These letters stand for 'JAXB Marshalling'. You should now see the following:
A red underline appears, because the characters you typed do not form a word that is part of the Java programming language. Instead, these letters form a NetBeans code template, which we will use in the next step. A related code template is 'jaxbu', which stands for 'JAXB Unmarshalling'.
Press the 'Tab' key.The 'jaxbm' characters expand and a code snippet appears:
try {
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(cr.getClass().getPackage().getName());
javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(cr, System.out);
} catch (javax.xml.bind.JAXBException ex) {
// XXXTODO Handle exception
java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
}
Right-click anywhere in the body of the code and select Fix Imports. The necessary package import statements are added to the code. Any error indicators should disappear.
You have now used various tools provided by the IDE for working with JAXB in the editor. Your small application is now ready for deployment.
If you are using NetBeans IDE 6.5, right-click the project node and select Properties from the context menu. The Project Properties dialog opens. Navigate to Build > Compiling and unselect Compile on Save. Otherwise, you will get Class Not Found exceptions for your generated classes.
Run the application. The Output window displays the following:
For more information about using NetBeans IDE to develop Java EE applications, see the following resources:
To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.
You are viewing a mobilized version of this site...
View original page here