6

我正在使用 ColdFusion 10 的新内置 REST API,我想返回状态代码 201(已创建)。我首先尝试了此处描述的 RestSetResponse() 方法:http: //www.adobe.com/devnet/coldfusion/articles/restful-web-services.html。它运行良好,除了它强制您将函数的返回类型设置为“void”。“void”的问题是,每当我抛出异常时,它不再返回正确的 JSON 错误消息。

抛出此异常:

<cfthrow errorcode="400" message="Validation error." />

当返回类型为“struct”时,返回格式良好的 JSON:

HTTP/1.1 400 Bad Request
Content-Type: application/json

{"Message":"Validation error."}

但是当返回类型为“void”时(这是使用 RestSetResponse() 所必需的),响应是一些丑陋的 HTML 响应。

因此,我不得不恢复使用返回类型“struct”,放弃了 RestSetResponse(),并尝试了这个:

<cfheader statusCode="201" statusText="Created" />

但它不起作用。似乎 ColdFusion 会覆盖 statusCode 并在成功时始终返回 200(OK)。任何人都知道在不将函数的返回类型设置为“void”的情况下将状态码更改为 201 的方法吗?

4

1 回答 1

1

我看不出为什么restSetResponse()需要返回类型为 void 的充分理由,但已经证实如果不是这种情况,它会被忽略。这有点垃圾。

The only thing I can think by way of working around your situation is to roll-your-own struct with the error detail in it, then use that as the content value set for the restSetResponse() call.

This is a bit jerry-built, but you're constrainted by the jerry-built-ness of ColdFusion in this instance, I think.

I've logged a bug relating to this.

于 2013-03-31T01:35:32.207 回答