JAXB Model (Auto) Creation
Before you can develop a REST client you need to generate a JAXB model for the remote service. The model classes can be generated from an XML Schema specifying the service's response XML. Often a Schema is not provided, however it can usually be automatically generated from a sample response.
As example we use the GoGreen REST API to retrieve (top 10 product) documents:
This URL returns XML by default but you can specify JSON as well:
Copy REST Response XML to Project
Save the XML response as an XML file in your project:
Generate XML Schema
Save the Schema in your project:
Generate JAXB Java Classes from XML Schema
Finally generate JAXB classes from Schema. Again many IDEs offer this functionality built-in or through a plugin, or you can use the command line tool xjc.
If you use xjc, create a package org.example.jaxb in the site module of your project ( site/src/main/java/org/example/jaxb) and run the following command from your project's root directory:
xjc site/src/main/resources/model.xsd -d site/src/main/java/org/example/jaxb
You will see the following output:
parsing a schema... compiling a schema... generated/ObjectFactory.java generated/Products.java
The generated Java classes will be placed in the package org.example.jaxb.generated.
Move the two classes to org.example.jaxb and remove the generated directory.