Advertise with us

Search Mastertheboss

Our eBooks

SwitchYard tutorial


So far we have consumed Services from a JSF page. It is however possible to consume them by means of other protocols. Transformation represents a change to the format and/or representation of a message's content.  The representation of a message is simply the Java contract (e.g. java.lang.String, org.example.MyFancyObject) used to access the underlying content.  The format of a message refers to the actual structure of the data itself.  Examples of data formats include XML, JSON, CSV, and EDI.

Transformers can be applied to a Java class using the org.switchyard.annotations.Transformer annotation which defines an incoming channel (from parameter) and an outgoing channel (to parameter).


package com.sample;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;

import org.switchyard.annotations.Transformer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class Transformers {
    // Transforms the incoming XML message to Calculator
    @Transformer(from = "{urn:switchyard-quickstart-demo:math:1.0}addition")
    public Calculator transform(Element from) {
        Calculator calc = new Calculator();

        calc.setNumber1(Integer.parseInt(getElementValue(from, "number1")));
        calc.setNumber2(Integer.parseInt(getElementValue(from, "number2")));
        return calc;
    // Transforms the Result class into an XML response
    @Transformer(to = "{urn:switchyard-quickstart-demo:math:1.0}additionResponse")
    public Element transform(Result result) {
        StringBuffer ackXml = new StringBuffer()
            .append("<math:additionResponse xmlns:orders=\"urn:switchyard-quickstart-demo:math:1.0\">")
            .append("<total>" + result.getTotal() + "</total>")

        return toElement(ackXml.toString());

    private String getElementValue(Element parent, String elementName) {
        String value = null;
        NodeList nodes = parent.getElementsByTagName(elementName);
        if (nodes.getLength() > 0) {
            value = nodes.item(0).getChildNodes().item(0).getNodeValue();
        return value;

    private Element toElement(String xml) {
        DOMResult dom = new DOMResult();
        try {
                    new StreamSource(new StringReader(xml)), dom);
        } catch (Exception ex) {

        return ((Document)dom.getNode()).getDocumentElement();

The "to" parameter can be omitted is the outgoing channel is a concrete java type. Conversely, you can omit the "from" if the incoming channel is a concrete Java type

The public Calculator transform(Element from) method will transform from a DOM element to a Calculator instance.

jboss soa switchyard tutorial esb
The public Element transform(Result result) converts from a Result java class to an XML element.
jboss soa esb switchyard
Since transformations occur between named types (i.e. from type A, to type B), it's important to understand how the type names are derived.  The type of the message is determined based on the service contract, which can be WSDL or Java.

In this example, the message name is determined based on the fully-qualified element name of a WSDL message.  Take the following WSDL definition (we have used wsprovide tool in JBOSS_HOME/bin to generate the WSDL see this link):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <xs:element name="addition" type="tns:additionType"/>
        <xs:element name="additionResponse" type="tns:additionResponseType"/>
        <xs:element name="calculator" type="tns:calculatorType"/>
        <xs:element name="result" type="tns:resultType"/>
        <xs:complexType name="additionType">
                <xs:element name="calculator" type="tns:calculatorType"/>
        <xs:complexType name="additionResponseType">
                <xs:element name="result" type="tns:resultType"/>
        <xs:complexType name="calculatorType">
                <xs:element name="number1" type="xs:int"/>
                <xs:element name="number2" type="xs:int"/>
        <xs:complexType name="resultType">
                <xs:element name="total" type="xs:int"/>
  <message name="addition">
    <part name="parameters" element="tns:addition"/>
  <message name="additionResponse">
    <part name="parameters" element="tns:additionResponse"/>
  <portType name="MathService">
    <operation name="addition">
      <input message="tns:addition"/>
      <output message="tns:additionResponse"/>
  <binding name="MathServiceBinding" type="tns:MathService">
    <soap:binding transport="" style="document"/>
    <operation name="addition">
      <soap:operation soapAction="urn:switchyard-quickstart-demo:math:1.0"/>
        <soap:body use="literal"/>
        <soap:body use="literal"/>
  <service name="MathService">
    <port name="MathServicePort" binding="tns:MathServiceBinding">
      <soap:address location="http://localhost:18001/test"/>

Finally, you need to declare your Web service SOAP binding in your switchyard.xml file and the Transformers which will be used to convert the messages from one channel to another:

<?xml version="1.0" encoding="UTF-8"?>
<switchyard xmlns="urn:switchyard-config:switchyard:1.0" name="math" targetNamespace="urn:switchyard-quickstart-demo:math:0.1.0">
    <composite xmlns="" name="math" targetNamespace="urn:switchyard-quickstart-demo:math:0.1.0">
        <service name="MathService" promote="MathService">
            <binding.soap xmlns="urn:switchyard-component-soap:config:1.0">
        <component name="MathService">
            <implementation.bean xmlns="urn:switchyard-component-bean:config:1.0" class="com.sample.MathServiceBean"/>
            <service name="MathService">
                < interface="com.sample.MathService"/>
        < xmlns="urn:switchyard-config:transform:1.0" class="com.sample.Transformers" from="{urn:switchyard-quickstart-demo:math:1.0}addition" to="java:com.sample.Calculator"/>
        < xmlns="urn:switchyard-config:transform:1.0" class="com.sample.Transformers" from="java:com.sample.Result" to="{urn:switchyard-quickstart-demo:math:1.0}additionResponse"/>

Fine, now deploy your Web service. From the server logs, you will see that your Web service has been published at the socket address defined in your switchyard.xml file:


21:59:21,727 INFO  [org.switchyard.component.soap.InboundHandler] (MSC service thread 1-7) Publishing WebService at


Now you can use any SOAP Web service client to test your Web service against the WSDL which is reachable at
Here's the SOAP outgoing message:

<soapenv:Envelope xmlns:soapenv="" xmlns:urn="urn:switchyard-quickstart-demo:math:1.0">

And this is the returning SOAP message:

   <SOAP-ENV:Envelope xmlns:SOAP-ENV="">
      <math:additionResponse xmlns:math="urn:switchyard-quickstart-demo:math:1.0">

In the next tutorial we will show how to use an Eclipse plugin (SOAP Ui) to invoke and test Web services. Stay tuned !

© 2020 All Rights Reserved.

Please publish modules in offcanvas position.