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:
https://www.demo.onehippo.com/restapi/topproducts?_type=xml
This URL returns XML by default but you can specify JSON as well:
https://www.demo.onehippo.com/restapi/topproducts?_type=json
Copy REST Response XML to Project
Save the XML response as an XML file in your project:
site/src/main/resources/model.xml
Generate XML Schema
You can now generate an XML Schema from model.xml. Many IDEs (e.g. IntelliJ) have built-in tools to do this, or you can use the online XSD/XML Schema Generator at freeformatter.com:
Save the Schema in your project:
site/src/main/resources/model.xsd
Generate JAXB Java Classes from XML Schema
Finally, generate JAXB classes from the 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.