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

Difference between revisions of "Stardust/Knowledge Base/Integration/Data/Structured Data to Java Conversion with JAXB"

m
m
Line 11: Line 11:
 
== Solution  ==
 
== 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 [[STP/Stardust/KnowledgeBase/BuildChangeMgmt/UtilityProjects|Utility Project]] with a [[Stardust/Knowledge_Base/Build_and_Change_Management/Maven|Maven]] configuration.  
+
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 [[STP/Stardust/KnowledgeBase/BuildChangeMgmt/UtilityProjects|Utility Project]] with a [[Stardust/Knowledge Base/Build and Change Management/Maven|Maven]] configuration.  
  
 
#Create an XML Schema (XSD)  
 
#Create an XML Schema (XSD)  
Line 19: Line 19:
 
#Tie everything together via the IN/OUT data mappings in the Stardust model
 
#Tie everything together via the IN/OUT data mappings in the Stardust model
  
=== 1. XML Schema  ===
+
=== Create an XML Schema  ===
  
 
This is a super simple XSD, but it's enough to showcase the functionality  
 
This is a super simple XSD, but it's enough to showcase the functionality  
Line 40: Line 40:
 
</source>  
 
</source>  
  
Store this XSD in your utility project as '''/src/java/resources/xsd/Customer.xsd'''
+
Store this XSD in your utility project as '''/src/java/resources/xsd/Customer.xsd'''  
  
=== 2. Generate Java Classes  ===
+
=== Generate Java Classes  ===
  
To generate the Java classes from the schema we're using the JAXB Maven Plug-In
+
To generate the Java classes from the schema we're using the JAXB Maven Plug-In  
  
=== 3. Create Structured Data Types ===
+
=== Create Structured Data Type Definitions ===
  
=== 4. JAXB marshalling/unmarshalling  ===
+
Create Structured Data Type definitions in Stardust by importing complex types from the XSD.  
  
=== 5. Invoking the Java Application from Stardust ===
+
*In the Outline View of the Stardust process model right-klick on '''Structured Data Types''' and choose '''Import'''
 +
*sdfs
 +
 
 +
=== JAXB marshalling/unmarshalling  ===
 +
 
 +
To marshal/unmarshal your code you need some boilerplate JAXB routines.  
 +
 
 +
=== Invoking the application from Stardust ===

Revision as of 12:32, 16 March 2012

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 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

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
  • sdfs

JAXB marshalling/unmarshalling

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

Invoking the application from Stardust

Back to the top