When you are creating REST APIs with WSO2 ESB, you may need to send some response message back to the user when something goes wrong. In this kind of scenario, you can create a payload inside the ESB and send it back. For doing this, you can use the below configuration.
<api xmlns="http://ws.apache.org/ ns/synapse" name="LoopBackProxy" context="/loopback">
<resource methods="POST GET">
<inSequence>
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"></property>
<log level="full"></log>
<payloadFactory media-type="xml">
<format>
<m:messageBeforeLoopBack xmlns:m="http://services. samples">
<m: messageBeforeLoopBackSymbol>
<m:symbolBeforeLoopBack>$1</m: symbolBeforeLoopBack>
</m: messageBeforeLoopBackSymbol>
</m:messageBeforeLoopBack>
</format>
<args>
<arg xmlns:ns="http://org.apache. synapse/xsd" xmlns:m0="http://services. samples/xsd" evaluator="xml" expression="//m0:symbol/text() "></arg>
</args>
</payloadFactory>
<loopback></loopback>
</inSequence>
<outSequence>
<payloadFactory media-type="xml">
<format>
<m:messageAfterLoopBack xmlns:m="http://services. samples">
<m:messageAfterLoopBackSymbol>
<m:symbolAfterLoopBack>$1</m: symbolAfterLoopBack>
</m: messageAfterLoopBackSymbol>
</m:messageAfterLoopBack>
</format>
<args>
<arg xmlns:ns="http://org.apache. synapse/xsd" xmlns:m0="http://services. samples" evaluator="xml" expression="//m0: symbolBeforeLoopBack/text()">< /arg>
</args>
</payloadFactory>
<property name="ContentType" value="text/xml" scope="axis2"></property>
<send></send>
</outSequence>
</resource>
</api>
In the above configuration, the most important part is the removal of NO_ENTITY_BODY property. The reason for removing this property is that in this scenario, request is not going to the backend and the GET request will set the "NO_ENTITY_BODY" property to true (default value). This will make sure that the request does not have a message body since this is a GET request.
<api xmlns="http://ws.apache.org/
<resource methods="POST GET">
<inSequence>
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"></property>
<log level="full"></log>
<payloadFactory media-type="xml">
<format>
<m:messageBeforeLoopBack xmlns:m="http://services.
<m:
<m:symbolBeforeLoopBack>$1</m:
</m:
</m:messageBeforeLoopBack>
</format>
<args>
<arg xmlns:ns="http://org.apache.
</args>
</payloadFactory>
<loopback></loopback>
</inSequence>
<outSequence>
<payloadFactory media-type="xml">
<format>
<m:messageAfterLoopBack xmlns:m="http://services.
<m:messageAfterLoopBackSymbol>
<m:symbolAfterLoopBack>$1</m:
</m:
</m:messageAfterLoopBack>
</format>
<args>
<arg xmlns:ns="http://org.apache.
</args>
</payloadFactory>
<property name="ContentType" value="text/xml" scope="axis2"></property>
<send></send>
</outSequence>
</resource>
</api>
In the above configuration, the most important part is the removal of NO_ENTITY_BODY property. The reason for removing this property is that in this scenario, request is not going to the backend and the GET request will set the "NO_ENTITY_BODY" property to true (default value). This will make sure that the request does not have a message body since this is a GET request.
Comments
Post a Comment