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 to do it the other way around, because the generated XML Schema is often not optimal and needs to be adapted. So in this article the XML Schema definition will 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

Create an XML Schema

This is a very simple XSD, but it's enough to showcase the functionality. It defines a complex type called "Customer".

<?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="long"></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

Generate Java Classes

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

Create Structured Data Type Definitions

Create Structured Data Type definitions in Stardust by importing complex types from the XSD.

  • In the Outline View of the Stardust process model right-klick on Structured Data Types and choose Import Types
  • If you're process model resides in the same project, select Workbench Projects > Next ... then navigate to resources/xsd/Customer.xsd
  • Select the CustomerType like depicted in the screenshot below
  • Hint: Do NOT select "Save original schema in the model". This will allow you to make changes to the XSD later and they will be reflected in the model after closing and re-opening it.

Stardust-Import-structured-data-from-xsd.png

JAXB marshalling/unmarshalling

To marshal/unmarshal your code you need some boilerplate JAXB routines.

Invoking the application from Stardust

Back to the top