...
----invoking updateCustomer, Customer name is: ESB
Handling non-matching resources
In this scenario, we are defining a sequence to be invoked if the WSO2 ESB is unable to find a matching resource definition for a specific API invocation. This sequence generates a response indicating an error when no matching resource definition is found.
Setting up the back end
For the back-end service, we are using the StockQuote Service that is shipped with ESB. cURL is used as the REST client to invoke the ESB API. Run Sample 800 as follows.
For further information on how to run an ESB sample, see ESB Samples Setup.
- Execute one of the following commands to start the ESB with the configuration for sample 800.
- On Windows:
wso2esb-samples.bat -sn 800
- On Linux/Solaris: ./
wso2esb-samples.sh -sn 800
- On Windows:
- Execute one of the following commands to start the the Axis2 server.
- On Windows:
axis2server.bat
On Linux/Solaris: ./
axis2server.sh
- On Windows:
- Run
ant
from the<ESB_HOME>/samples/axis2Server/src/SimpleStockQuoteService
directory.
Configuring the API
Create an API using the following configuration:
Code Block | ||
---|---|---|
| ||
<api xmlns="http://ws.apache.org/ns/synapse" name="jaxrs" context="/jaxrs">
<resource methods="GET" uri-template="/customers/{id}">
<inSequence>
<send>
<endpoint>
<address uri="http://localhost:9764/jaxrs_basic/services/customers/customerservice"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
</api> |
Creating the sequence
Create a new sequence with the following configuration:
Code Block | ||
---|---|---|
| ||
<sequence xmlns="http://ws.apache.org/ns/synapse" name="_resource_mismatch_handler_">
<payloadFactory>
<format>
<tp:fault xmlns:tp="http://test.com">
<tp:code>404</tp:code>
<tp:type>Status report</tp:type>
<tp:message>Not Found</tp:message>
<tp:description>The requested resource (/$1) is not available.</tp:description>
</tp:fault>
</format>
<args>
<arg xmlns:ns="http://org.apache.synapse/xsd" expression="$axis2:REST_URL_POSTFIX"/>
</args>
</payloadFactory>
<property name="RESPONSE" value="true" scope="default"/>
<property name="NO_ENTITY_BODY" action="remove" scope="axis2"/>
<property name="HTTP_SC" value="404" scope="axis2"/>
<header name="To" action="remove"/>
<send/>
<drop/>
</sequence> |
Executing the sample
Send an invalid request to the back end as follows:
Code Block | ||
---|---|---|
| ||
curl -X GEThttp://localhost:8280/jaxrs/customers-wrong/123 |
You will get the following response:
Code Block | ||
---|---|---|
| ||
<tp:fault xmlns:tp="http://test.com">
<tp:code>404</tp:code>
<tp:type>Status report</tp:type>
<tp:message>Not Found</tp:message>
<tp:description>The requested resource (//customers-wrong/123) is not available.</tp:description>
</tp:fault> |
Excerpt | ||
---|---|---|
| ||
Configuring REST API use cases in WSO2 ESB |