使 Alfresco Web 脚本将错误 500 显示为 JSON 而不是 HTML

Make Alfresco Web Script show error 500 as JSON rather than HTML

我正在编写将由外部计算机使用的 Alfresco Web 脚本。

当我抛出 WebScriptException 时,我希望错误 500 在 JSON 中带有错误页面,而不是 HTML,以便外部计算机可以解析它。现在我得到这个:

如何获取 JSON?

我看到一些 Web 脚本设法生成错误页面 JSON(不过对于错误 401,所以可能在实际调用 Web 脚本之前):

您可以为每个状态代码创建一个专用的自由标记。如果您转到此网页 http://docs.alfresco.com/5.2/concepts/ws-component-name.html 并滚动到 "FreeMarker Templates - Response status",您可以看到一些示例。

引用自 5.2:

Response status code document file names adhere to a naming convention as defined by the Web Script Framework. The appropriate response status code template is searched for in the following order:

  • A template located in the same folder as the web script description document for rendering a specific status code response, which adheres to the naming convention <web script id>.<http method>.<format>.<status code>.ftl
  • A template located in the same folder as the web script description document for rendering a response of any status code, which adheres to the naming convention <web script id>.<http method>.<format>.status.ftl
  • A package-level template located in the package of the web script but, if not found, is searched for in the parent package hierarchy, up to the root package for rendering a response of any status code, which adheres to the naming convention <format>.status.ftl
  • A template located in the root package for rendering an HTML response for the specific status code, which adheres to the naming convention <status code>.ftl
  • A template located in the root package for rendering an HTML response of any status code, which adheres to the naming convention: status.ftl