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

孤人 提交于 2019-12-12 06:58:42

问题


I am writing an Alfresco Web Script that will be consumed by external computers.

When I throw a WebScriptException, I would like the error 500 to come with an error page in JSON, rather than HTML, so that the external computers can parse it. Right now I am getting this:

How to get JSON instead?

I see that some Web Scripts manage to produce error pages as JSON (for error 401 though, so probably before the Web Script actually gets called):


回答1:


You can create a freemarker dedicated for each status code. If you go to this web page http://docs.alfresco.com/5.2/concepts/ws-component-name.html and scroll to "FreeMarker Templates - Response status", you can see some examples.

Quoting as of 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


来源:https://stackoverflow.com/questions/42431002/make-alfresco-web-script-show-error-500-as-json-rather-than-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!