Hi @vhochsteinTef ,
I think this is behavior in AEM as a Cloud Service with Dispatcher: it intercepts 404 status codes and redirects to the AEM error handler (like /content/404.html), instead of returning your custom JSON response.
You can try to follow these steps -
1. Update Dispatcher Config to Allow Error Passthrough
# Allow all responses from your servlet, including 4xx and 5xx
/0200 {
/type "allow"
/method "GET"
/url "/bin/myapi/.*"
/status "400-599"
}
- /status "400-599" → Allows Dispatcher to pass through error status codes
- /url → Should match your servlet path (e.g. /bin/myapi/something)
2. Ensure servlet sets status & content-type correctly
response.setStatus(HttpServletResponse.SC_NOT_FOUND); // 404
response.setContentType("application/json");
response.getWriter().write("{\"error\": \"Item not found\"}");
3. Optionally Bypass AEM Global Error Handling - AEM’s default Sling error handler may still override responses. If needed:
- Overlay /apps/sling/servlet/errorhandler/404.jsp
- Detect JSON path or content-type and exit early, not rendering HTML.
Let me know if this process works.