WSO2 ESB provides us luxury to create a custom functionality in java as class mediator. In class mediator we can write our desired functionality in java class and that can be called from the ESB synapse file. We can pass some property from ESB file to class mediator and in a same way we can set some property in java class and that can be read from synapse file.
Here is an example in which I need to reply a static response to customer. However this can be done from many ways but I am choosing class mediator just to show how this can be done from class mediator.
I want below static response to be shown in as actual response of the service. I have put this “StaticEmployeeDetails.xml” file in “XX\wso2esb-4.0.3\repository\conf\stub\StubXml\ StaticEmployeeDetails.xml” location.
Static XML Code:
Here is the Class mediator code which will be called from the wso2 Synapse file.
Class Mediator Code:
- · Different property has been used in above code just to show that what all we can set from class mediator and use it in our synapse file.
- · Create ja jar file of this above code and place it in the “X\ wso2esb-4.0.3\repository\components\lib\CustomMediator.jar”
- · Now we need to provide the reference of this class from the synapse.xml file
Java Script Code:
I am using a java script code which will be called from synapse.xml file. This will create a string text to xml content.
Just restart the server to reflect the changes and hit the “MyCustomProxy” service and you will get the desired response. If you want to change the static response just change the content of the “StaticEmployeeDetails.xml” file located inside of “wso2esb-4.0.3\repository\conf\stub\StubXml” and restart the server.