Make Alfresco Web Script show error 500 as JSON ra

2019-09-15 16:57发布

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:

Web Script error 500

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):

enter image description here

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-15 17:03

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
查看更多
登录 后发表回答