Q:

Mulesoft API how to return 404 if no records found

I'm using Mulesoft Anypoint to create REST API end-points.  I have a resource for which my call to the backend database returns no records.  
E.g. http://myhostname/players/1234 - where player 1234 does not exist in the system.
My understanding is that for a REST based approach this should return a 404 as the requested resource (player 1234) does not exist.
I'm unclear how to handle this response in my flow in Anypoint Studio, based on receiving a json document from my backend with an empty array, how to convert this into a 404 response to return using APIKit router

A:

I assume you have specified your interface with a RAML file and you have generated flows from that.
Place a validator in the flow where you have the array with results or the empty array:
<validation:is-not-empty message="Not Found" 
      exceptionClass="org.mule.module.apikit.exception.NotFoundException" 
      value="#[payload]" doc:name="Validate not empty"/>

The generated exception mapping converts the NotFoundException to a 404. You can configure the excact answer in the exception mapping. The default is a JSON document { "message": "Resource not found" }

