Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Stardust/Knowledge Base/Integration/Data/Structured Data to Java Conversion with JAXB

Synopsis

This article describes how to use JAXB to marshal/unmarshal between XML data and Java objects and leverage this technology in combination with Structured Data Types in Stardust.

Examples implemented with Stardust 7.0.

Problem Description

Using Structured Data Types in the Stardust process model is the most common approach for representing complex data objects. Very often XML Schemas already exist and are imported into Stardust to create the Structured Data Definitions. However, when passing a Structured Data object into a Java class during the process flow, Stardust by default converts it into a java.util.Map object which is cumbersome to deal with, because the developer has to know the keys for the value fields. It would be preferrable if the Java developer could deal with real POJOs inside the Java code while still representing these objects as Structured Data in the Stardust model.

Solution

Using JAXB you can link XML Schema definitions to POJOs and convert from/to Structured Data (almost) automatically when invoking Java Applications or Spring Beans. Personally I find it easier to start off with an XML Schema and generate the Java classes than the other way around, because the generated XML Schema is often not optimal. So in this article the XML Schema definition will be the contain the leading data model from which everything else is generated. The examples are based on using a Utility Project with a Maven configuration.

  1. Create an XML Schema (XSD)
  2. Generate Java classes and bindings using JAXB
  3. Import the XSD into Stardust to create Structured Data Definitions
  4. Leverage some JAXB boilerplate code for marshalling/unmarshalling
  5. Tie everything together via the IN/OUT data mappings in the Stardust model

1. XML Schema

This is a super simple XSD, but it's enough to showcase the functionality

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
	targetNamespace="http://www.example.org/Customer"
	xmlns:tns="http://www.example.org/Customer"
	elementFormDefault="qualified">
 
    <complexType name="CustomerType">
    	<sequence>
            <element name="id" type="string"></element>
            <element name="firstname" type="string"></element>
    	    <element name="lastname" type="string"></element>
    	</sequence>
    </complexType>
</schema>

Store this XSD in your utility project as /src/java/resources/xsd/Customer.xsd

2. Generate Java Classes

To generate the Java classes from the schema we're using the JAXB Maven Plug-In

3. Create Structured Data Types

4. JAXB marshalling/unmarshalling

5. Invoking the Java Application from Stardust

Back to the top