Skip to Content
Author's profile photo Former Member

Step-by-Step to learn Java-Mapping with JAXB

Step-by-Step learn JAXB Mapping

Based on this Blog I began to learn JAXB-Mapping:

This blog gives not every detail and I’m new comer in Java World, therefore it is not easy to finish complete scenario. After hard working I made it and would like to share my experience with you here.

In the above link you can find theories about JAXB. Here I just show what I did in NWDS and PI.

1.   1.  2 external Definitions
ED_EmpSource in Namespace:; and ED_EmpTarget in Namespace:
Why I do so?
At beginning I tried to use Message Types within one namespace, but this makes some difficulties, i.e. variable names are same and conflict in the same package name.
second reasons: the Error: “unable to marshal type because it is missing an @XmlRootElement annotation”
For more information please refer to the article:
Therefore the structure of the XSD should look as below: 01_XSD.jpg

As As mentioned in this article: Make sure that the root element is an anonymous complex type and not an instance of a defined type.
The Message Type in PI looks normally like this:

With it you will encounter this XmlRootElement error.

2.   2.  Finish PI Scenario in ESR with Service Interface, Message Mapping and Operation Mapping.

The above activities are classic PI configuration.

What I want to do is concat firstname and lastname into fullname when the country is “India” and I am going to use JAXB Mapping to replace graphic mapping. 

3.   3.  Export 2 external definitions in XSD files locally.

4.   4. Set up external tool in NWDS xjc:
/wp-content/uploads/2012/03/04_xjc_81545.jpg /wp-content/uploads/2012/03/05_xjc_81546.jpg

5.   5. Create a new Java Project: JAXB_Test_Emp
and import the following external jar libraries: 

6.    6. Create 2 directories: sourceData and targetData

7.  7.   Copy-paste XSDs into the corresponding directories. 

8.   8. Using XJC to generate the required java classes related to the source and target XSD.
The details of these operations:
click on ource XSD and mark it and then start XJC:

9.   9. Make the generate java classes to Source in the project properties. 09_Source.jpg
After that the source and target directories are changed to internal source directory or packages, that you can call them in you java class. /wp-content/uploads/2012/03/10_javaclasses_81552.jpg

10.  10. Create a new package “jaxbtest” and a new class “MM_JAXB_Mapping” /wp-content/uploads/2012/03/11_javaclass_81553.jpg/wp-content/uploads/2012/03/12_addinterface_81557.jpg

11. 11. The codes in MM_JAXB_Mapping Class look like this:

package jaxbtest;





import java.util.List;

import java.util.Map;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBElement;

import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;


import com.dong.javamapping.DTEmp;

import com.dong.jaxbtest.MTEmpTarget;






* @author Rene Dong JAXB Mapping Test in PI 14.03.2012, Germany


public class MM_JAXB_Mapping extends AbstractTransformation {

   InputStream is = null;

   public void transform(TransformationInput arg0, TransformationOutput arg1)

               throws StreamTransformationException {

         getTrace().addInfo(“JAVA Mapping MM_JAXB_Mapping is Initiated”);

         try {

               is = arg0.getInputPayload().getInputStream();

               JAXBContext jaxbContext = JAXBContext.newInstance(DTEmp.class);

               Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();

               JAXBElement<DTEmp> employee_source = (JAXBElement<DTEmp>) unMarshaller

                           .unmarshal(new StreamSource(is), DTEmp.class);

               getTrace().addInfo(“Unmarshall Successful”);

               /*————- Marshal the Target—————-*/

               com.dong.jaxbtest.MTEmpTarget target = new MTEmpTarget();

               List<DTEmp.Employee.Name> EmployeeS = employee_source.getValue()



                           “Source Employee has (“ + EmployeeS.size()

                                       + “) Employee Knots”);

               for (int i = 0; i < EmployeeS.size(); i++) {

                     DTEmp.Employee.Name NameS = (DTEmp.Employee.Name) EmployeeS


                     String c = NameS.getCountry();

                     if (c.equalsIgnoreCase(“india”)) {

                           MTEmpTarget.Employee emp = new MTEmpTarget.Employee();

                           emp.setFullname(NameS.getFirstname() + ” “

                                       + NameS.getLastname());





               JAXBContext contextTarget = JAXBContext


               Marshaller marshallerTarget = contextTarget.createMarshaller();

               marshallerTarget.marshal(target, arg1.getOutputPayload()


         } catch (Exception e) {




   public String convertInputStreamToString(InputStream in) {

         StringBuffer sb = new StringBuffer();

         try {

               InputStreamReader isr = new InputStreamReader(in);

               Reader reader = new BufferedReader(isr);

               int ch;

               while ((ch = > -1) {

                     sb.append((char) ch);



         } catch (Exception exception) {


         return sb.toString();



    * (non-Javadoc)


    * @see


   public void setParameter(Map arg0) {



12. 12. Export the whole project as a jar file and save it locally.

13.  13.Import jar file as imported Archive into PI. 

14.  14. Replace the Message-Mapping in the Operation Mapping with the new Imported Archive.  16_OM.jpg

15.15.  Test in the operation mapping

So it works!

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Otto Frost
      Otto Frost

      Instead of using the xjc for generation, you can use the SAP graphical wizards.

      In ESR perspective, click the service interface

      Generate Client

      follow the wizard to generate the artifacts.

      sometimes it is necessary to mess with the classloader, otherwise the marshalling unmarshalling won't work or it is impossinle to create jaxb context


      ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
        try {
      // JAXB coding and mapping

      String contextPath = ItemType.class.getPackage().getName();

      JAXBContext jc = JAXBContext.newInstance(contextPath);

      // more coding

        } finally {

      using the wizard avoids using jaxb customizing which can be tricky